C# Converting a Currency String to Double - c#

I have a basic understanding of C# and the .NET Framework, I have been given an assignment to build a POS (Point Of Sales) screen, I have currently hit a small brick wall trying to convert a currency related string back to a double.
I have two list boxes and several product buttons on the screen, the buttons are populated using a library class provided to us (essentially showing we can work with components)
One list box holds the product name while the other holds the price of that product, when a product button is selected it takes the product name from the buttons text and within its tag there is the price which is added to the list box of prices.
my problem is I want to show the prices in the List Box as a currency also that it shows all '0' I can do this no problem by doing either the following
value.ToString("C");
string.Format("{0:C}",value);
or using Convert etc.
Although because I have done this if I want to remove an item from the list by double clicking I need to take away the price from the total so I need to convert to back to double although because its in its current format I get an error when trying to perform that action I have looked around and I cannot seem to find anyway around it, the only option I can see is just leaving the string value as it is and not convert it to a currency format.
the ERROR: {"Input string was not in a correct format."}
Code Snippet
private void panelBtns_Click(object sender, EventArgs e)
{
Button panelBtn = (Button)sender;
lstProduct.Items.Add(panelBtn.Text);
double price = Convert.ToDouble(panelBtn.Tag);
>>CURRENCY FORMAT>> lstPrice.Items.Add(string.Format("{0:C}",price));
dblTotal = dblTotal + Convert.ToDouble(panelBtn.Tag);
lblTotal.Text = string.Format("{0:C}", dblTotal);
lblOutput.Text = "0";
lblOutput.Tag = "0";
}//End Panel Buttons
private void lstProduct_DoubleClick(object sender, EventArgs e)
{
int index = lstProduct.SelectedIndex;
lstPrice.SelectedIndex = lstProduct.SelectedIndex ;
>> ERROR HERE >> double price = Convert.ToDouble(lstPrice.GetItemText(lstPrice.SelectedItem));
dblTotal = dblTotal - price;
lstProduct.Items.RemoveAt(index);
lstPrice.Items.RemoveAt(index);
lblTotal.Text = string.Format("{0:C}", dblTotal);
}
Would anyone have any idea how I could possibly fix this, I had though about creating an invisible list to store the actual value of the tag so I can use that for later but would there be any other methods?
NOTE: I am also aware that using double for currency is not a very reliable

The easiest way to parse the C format is probably with
Double.Parse(text, System.Globalization.NumberStyles.Currency)
Of course you would always want to use Decimal to handle currency, and Decimal.Parse takes the same parameters.
In this case, though, you would want to store your internal numeric values along with their textual representation rather than converting to a string and then parsing back into a number.

other way, but please note, that you must try all cultures if removing symbol gives just decimal string, use Gabe's answer (posted just before mine :D)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
//US-en culture / default I'm developing on
string currencyString = "$12.99"; //assuming got from: lstPrice.GetItemText(lstPrice.SelectedItem);
CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
double d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));
//using custom culture
currencyString = "12.99zł";
ci = CultureInfo.GetCultureInfo("PL-pl");
d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));
}
}
}

You may want to look at assigning the decimal too with Math.Round Method
https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx

Related

Format Currency string in c# without currency code

I am trying to format a double to currency string in c#
normally, I would use the following code:
using System;
using System.Globalization;
class Demo {
static void Main() {
double value = 234.66;
Console.WriteLine(value.ToString("C", CultureInfo.InvariantCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
}
}
issue:
The first format prepends an unwanted special caracter: ¤234.66
the later one pepends a dollar sign: $234.660
for normal usecases, I could use several culture infos such as in C# formatting currency given currency code (like USD / GBP / FRF)
unfortunately, Crypto currencies are not supported as far as I know of. So I either look for no currency symbol at all (adding it later to the string) or for a custom currency symbol.
What was quite close was to use balance.ToString("0.##") but in case of 104.10 it would make 104.1 out of it..
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.CurrencySymbol = "";
var currency = 104.67m;
var stringCurrency = currency.ToString("C", clone);

c# dictionary using textbox to select index from datagridview [duplicate]

I'm new with C#, I have some basic knowledge in Java but I can't get this code to run properly.
It's just a basic calculator, but when I run the program VS2008 gives me this error:
I did almost the same program but in java using JSwing and it worked perfectly.
Here's the form of c#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace calculadorac
{
public partial class Form1 : Form
{
int a, b, c;
String resultado;
public Form1()
{
InitializeComponent();
a = Int32.Parse(textBox1.Text);
b = Int32.Parse(textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
add();
result();
}
private void button2_Click(object sender, EventArgs e)
{
substract();
result();
}
private void button3_Click(object sender, EventArgs e)
{
clear();
}
private void add()
{
c = a + b;
resultado = Convert.ToString(c);
}
private void substract()
{
c = a - b;
resultado = Convert.ToString(c);
}
private void result()
{
label1.Text = resultado;
}
private void clear()
{
label1.Text = "";
textBox1.Text = "";
textBox2.Text = "";
}
}
What can be the problem? Is there a way to solve it?
PS: I also tried
a = Convert.ToInt32(textBox1.text);
b = Convert.ToInt32(textBox2.text);
and it didn't work.
The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.
It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.
I ran into this exact exception, except it had nothing to do with parsing numerical inputs. So this isn't an answer to the OP's question, but I think it's acceptable to share the knowledge.
I'd declared a string and was formatting it for use with JQTree which requires curly braces ({}). You have to use doubled curly braces for it to be accepted as a properly formatted string:
string measurements = string.empty;
measurements += string.Format(#"
{{label: 'Measurement Name: {0}',
children: [
{{label: 'Measured Value: {1}'}},
{{label: 'Min: {2}'}},
{{label: 'Max: {3}'}},
{{label: 'Measured String: {4}'}},
{{label: 'Expected String: {5}'}},
]
}},",
drv["MeasurementName"] == null ? "NULL" : drv["MeasurementName"],
drv["MeasuredValue"] == null ? "NULL" : drv["MeasuredValue"],
drv["Min"] == null ? "NULL" : drv["Min"],
drv["Max"] == null ? "NULL" : drv["Max"],
drv["MeasuredString"] == null ? "NULL" : drv["MeasuredString"],
drv["ExpectedString"] == null ? "NULL" : drv["ExpectedString"]);
Hopefully this will help other folks who find this question but aren't parsing numerical data.
If you are not validating explicitly for numbers in the text field, in any case its better to use
int result=0;
if(int.TryParse(textBox1.Text,out result))
Now if the result is success then you can proceed with your calculations.
Problems
There are some possible cases why the error occurs:
Because textBox1.Text contains only number, but the number is too big/too small
Because textBox1.Text contains:
a) non-number (except space in the beginning/end, - in the beginning) and/or
b) thousand separators in the applied culture for your code without specifying NumberStyles.AllowThousands or you specify NumberStyles.AllowThousands but put wrong thousand separator in the culture and/or
c) decimal separator (which should not exist in int parsing)
NOT OK Examples:
Case 1
a = Int32.Parse("5000000000"); //5 billions, too large
b = Int32.Parse("-5000000000"); //-5 billions, too small
//The limit for int (32-bit integer) is only from -2,147,483,648 to 2,147,483,647
Case 2 a)
a = Int32.Parse("a189"); //having a
a = Int32.Parse("1-89"); //having - but not in the beginning
a = Int32.Parse("18 9"); //having space, but not in the beginning or end
Case 2 b)
NumberStyles styles = NumberStyles.AllowThousands;
a = Int32.Parse("1,189"); //not OK, no NumberStyles.AllowThousands
b = Int32.Parse("1,189", styles, new CultureInfo("fr-FR")); //not OK, having NumberStyles.AllowThousands but the culture specified use different thousand separator
Case 2 c)
NumberStyles styles = NumberStyles.AllowDecimalPoint;
a = Int32.Parse("1.189", styles); //wrong, int parse cannot parse decimal point at all!
Seemingly NOT OK, but actually OK Examples:
Case 2 a) OK
a = Int32.Parse("-189"); //having - but in the beginning
b = Int32.Parse(" 189 "); //having space, but in the beginning or end
Case 2 b) OK
NumberStyles styles = NumberStyles.AllowThousands;
a = Int32.Parse("1,189", styles); //ok, having NumberStyles.AllowThousands in the correct culture
b = Int32.Parse("1 189", styles, new CultureInfo("fr-FR")); //ok, having NumberStyles.AllowThousands and correct thousand separator is used for "fr-FR" culture
Solutions
In all cases, please check the value of textBox1.Text with your Visual Studio debugger and make sure that it has purely-acceptable numerical format for int range. Something like this:
1234
Also, you may consider of
using TryParse instead of Parse to ensure that the non-parsed number does not cause you exception problem.
check the result of TryParse and handle it if not true
int val;
bool result = int.TryParse(textbox1.Text, out val);
if (!result)
return; //something has gone wrong
//OK, continue using val
In my case I forgot to put double curly brace to escape. {{myobject}}
You may encounter this exception when you use a string formatter with invalid bracket syntax.
// incorrect
string.Format("str {incorrect}", "replacement")
// correct
string.Format("str {1}", "replacement")
You have not mentioned if your textbox have values in design time or now. When form initializes text box may not hae value if you have not put it in textbox when during form design. you can put int value in form design by setting text property in desgin and this should work.
it was my problem too ..
in my case i changed the PERSIAN number to LATIN number and it worked.
AND also trime your string before converting.
PersianCalendar pc = new PersianCalendar();
char[] seperator ={'/'};
string[] date = txtSaleDate.Text.Split(seperator);
int a = Convert.ToInt32(Persia.Number.ConvertToLatin(date[0]).Trim());
I had a similar problem that I solved with the following technique:
The exception was thrown at the following line of code (see the text decorated with ** below):
static void Main(string[] args)
{
double number = 0;
string numberStr = string.Format("{0:C2}", 100);
**number = Double.Parse(numberStr);**
Console.WriteLine("The number is {0}", number);
}
After a bit of investigating, I realized that the problem was that the formatted string included a dollar sign ($) that the Parse/TryParse methods cannot resolve (i.e. - strip off). So using the Remove(...) method of the string object I changed the line to:
number = Double.Parse(numberStr.Remove(0, 1)); // Remove the "$" from the number
At that point the Parse(...) method worked as expected.

ASP.net ListBox currency formatting

Ok guys, I have a ListBox which displays products (they are a custom class and have ID, name, price) that are in a binding list.. I want the ListBox to display the item name AND the price. The listbox (lbProductsChosen) has "DataTextField" set to the Name and DataValueField set to the ID. I am using the PreRender event to check each Item, look at its price from the binding list (blProducts) etc. and it works great. I get the name and price displayed in the list with this. However, when it is displayed, despite me formatting it using String.Format, the result is still a decimal number (ex. 3.20000) and it just looks ugly. Does anyone know why its working to display it, but not displaying it how I want it formatted.
protected void lbProductsChosen_PreRender(object sender, EventArgs e)
{
foreach (ListItem item in lbProductsChosen.Items)
{
string currentDescription = item.Text;
int convertedValue = Convert.ToInt32(item.Value);
for (int i = 0; i < blProducts.Count; i++)
{
if (blProducts[i].ProductID == convertedValue)
{
decimal ItemPrice = blProducts[i].Price;
string convertedPrice = ItemPrice.ToString();
string currentPrice = String.Format("{0:c}", convertedPrice);
string currentDescriptionPadded = currentDescription.PadRight(30);
item.Text = currentDescriptionPadded + currentPrice;
}
}
}
}
MSDN states the following about the String.Format method.
Generally, objects in the argument list are converted to their string representations by using the conventions of the current culture, which is returned by the CultureInfo.CurrentCulture property.
If you use the currency format specifier c it will use the currency format which is defined in the system settings. Take a look under control panel -> region -> additional settings -> currency. Maybe you have messed up settings there.
If you want to ignore the local settings which would make sense for currency you could do the following. (example in dollar with allways two decimal places)
decimal price = 12.10999999m;
String.Format("${0:0.00}", price);
$12.10
Be aware of that String.Format doesn't round the number correctly but just cuts it off.

How can i convert string value to rial money unit?

I'm beginner in c# and have this value of string:
123456
but want convert that string to my country money, want convert that string value to this:
123,456
always split three numbers with comma for example, if string number is this:
1234567890
Show to user this:
1,234,567,890
How can i write code that purpose?
I would suggest convert it to int (or long) first and then use ToString() and supply required format.
int number = int.Parse(numberString); //ex..
number.ToString("N0"); // 1,000,000
If you're asking about culture-specific formatting, then you could do this.
number.ToString("N0", CultureInfo.CreateSpecificCulture("es-US"));
You can explore more on standard numeric formats
Example code
Use the standard formatters and the CultureInfo for the desired country.
e.g
int i = int.Parse("123456");
string money = i.ToString("C", CultureInfo.CreateSpecificCulture("fr-Ir"));
Or if the system culture is fr-Ir
string money = i.ToString("C");
Which is the same as
string money = i.ToString("C", CultureInfo.CurrentCulture);
Or if you want to use the UI culture (the culture of the requesting browser)
string money = i.ToString("C", CultureInfo.CurrentUICulture);
Since you want to convert your value to currency, I would suggest using "C" of string formats provided by .NET.
123456.125M.ToString("C"); // $123,456.13
Sign infront of the string will be defined by the culture of your machine. More information here.
On the other hand, there is another solution to add your own custom format:
123456.125M.ToString("#,0.################"); // 123,456.125
It is not the clean way, but I have not since found a correct way of actually formating this in generic way.
Side note: for currency handling it is generally considered a good practise to use decimal. Since it does not have a floating point issue.
Please try this one hope will help
Just whats inside the void method
using System.Linq;
public class Program
{
public void ABC()
{
var data = "123456789";
const int separateOnLength = N;
var separated = new string(
data.Select((x,i) => i > 0 && i % separateOnLength == 0 ? new [] { ',', x } : new [] { x })
.SelectMany(x => x)
.ToArray()
);
}
}

How do I validate a price (number) textbox?

I have a Windows forms application written in C#.
I am looking for a way to validate my price textBox so that it only accepts prices in a double format e.g. allowing 0.01 & 1200.00 but provides an error when the user enters characters.
I would except the code to looks similar to
String price = tbx_price.Text.Trim();
if price is not a number
{
error message
}
else{
...
What method could I use to check if the price string contains only numbers? Please note that I require the user to be able to use decimal places so the '.' character should be allowed.
Use decimal.TryParse :
decimal d;
if (!decimal.TryParse(price, out d)){
//Error
}
And if you also want to validate the price (145.255 is invalid):
if (!(decimal.TryParse(price, out d)
&& d >= 0
&& d * 100 == Math.Floor(d*100)){
//Error
}
You can test this using decimal.TryParse().
For example:
decimal priceDecimal;
bool validPrice = decimal.TryParse(price, out priceDecimal);
If you can't be sure that the thread culture is the same as the user's culture, instead use the TryParse() overload which accepts the culture format (also the number format which you can set to currency):
public bool ValidateCurrency(string price, string cultureCode)
{
decimal test;
return decimal.TryParse
(price, NumberStyles.Currency, new CultureInfo(cultureCode), out test);
}
if (!ValidateCurrency(price, "en-GB"))
{
//error
}
Besides using the answer marked as accepted in order to avoid the problem with culture about prices, you can always use this
Convert.ToDouble(txtPrice.Text.Replace(".", ","));
Convert.ToDouble(txtPrice.Text.Replace(",", "."));
this will depend how you manage your convertion in your app.
PS: I could not comment the answer because i do not have the neccesary reputation yet.

Categories