I have int variables, example:
int money = 1234567890;
How I can insert "." into money, and make its format like this:
1.234.567.890
You can simply do this:
var text = money.ToString("N0",
System.Globalization.CultureInfo.GetCultureInfo("de"));
The result is:
1.234.567.890
(I just picked the German culture as I knew they use . for the separator.)
You can use NumberFormatInfo.NumberGroupSeparator:
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
nfi.NumberGroupSeparator = ".";
Int64 myInt = 1234567890;
Console.WriteLine( myInt.ToString( "N", nfi ) );
(Link to ideone.)
To get exactly the format, use
int money = 1234567890;
money.ToString(#"#\.###\.###\.##0");
More information on custom formats here. You need to escape the dot because otherwise the first one will be interpreted as the decimal one. 0 in the end is necessary if you want to display it for zero values.
If you want a "Money' format try:
int money = 1234567890;
string moneyString = String.Format("{0:C}", money);
returns "$1,234,567,890.00"
Im not sure what money format uses '.' instead of ',' but that could just be a globalization thing.
Related
I have a question. how to changes "15.49" in string array to int value in c#?
Based on your comment, your culture uses "," as decimal separator, instead of ".". You could use overload of decimal.Parse to use custom culture format. Since you want to ignore the decimal part, you can use Int32.Convert to fetch the int part.
var stringValue = "15,49";
var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberDecimalSeparator = ",";
var result = Convert.ToInt32( decimal.Parse(stringValue, culture));
This would give your the required result
(int)decimal.Parse("15.49"); - if string null, parse return exception (maybe its what you want)
(int)Convert.ToDecimal("15.49"); if string null, convert return 0
If i am not wrong you want to convert string "15.49" into int ??? If yes then you can use this code Convert.toInt32("15.49");
I'm developing a website for a German Customer. In Germany, they use comma as decimal separator.
I use a WebMethod to get the values from SQL and then build a JSON object to show the data on the website. Using C# SqlCommand, I got the value from SQL as a string. I want to save this value into a double variable.
This is the code example which shows what I want to do:
NumberFormatInfo nfi = new CultureInfo( "de-DE", false ).NumberFormat;
nfi.NumberDecimalSeparator = ","; // Displays the value with a comma as the separator.
string value ="15.95"; //value from SQL
string valueInString = "";
double valueInDouble = 0;
valueInString = Convert.ToDouble(value.ToString()).ToString( "N", nfi );
valueInDouble = Convert.ToDouble(value.ToString()).ToString( "N", nfi ); //Error
valueInDouble = Convert.ToDouble(valueInString,nfi);
Console.WriteLine( valueInString ); // returns 15,95. But it is a string
Console.WriteLine( valueInDouble ); // returns 15.95. the comma is reverted back to dot
I need to save the data as double. How do I resolve this?
double does not contain any information about how to (visually) separate the decimal from the integral part. If you print a double without any specific culture format info, you get the standard . as a separator.
I can't figure out how to do the following :
I want to import some data from a file, including numeric values. The user can personalize separators, which are char. For exemple, a number may look like this : 2 524,2. Here, we have a "thousands" separator () and a "decimal" separator (,).
I try to convert these strings as double.
I know that I may do something like this :
double.Parse(str.Replace(tSep, '\0').Replace(dSep, '.'));
But I'm looking for a potential way of doing it more properly.
Thank you in advance.
Try this:
string s = "2 524,2";
CultureInfo ci = new CultureInfo(1);
NumberFormatInfo ni = new NumberFormatInfo();
ni.NumberGroupSeparator = " ";
ni.NumberDecimalSeparator = ",";
ci.NumberFormat = ni;
decimal d = decimal.Parse(s, ci);
double.Parse(str.Replace(' ', '\0').Replace(',', '.'));
is fine, but you should also set the culture to InvariantCulture
double.Parse(str.Replace(' ', '\0').Replace(',', '.',
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture));
in order to be sure that your code will work on every user culture setting
I get from a webservice the following strings:
12.95
or
1,200.99
Is there an option to convert these values to the following values without manipulating the string?
12,95
or
1200,99
I tried it with some Culture options but didn't get it right...
EDIT
I tried this:
//return string.Format( "{0:f2}", Convert.ToDecimal( price ) );
//return string.Format(CultureInfo.GetCultureInfo("de-de"), "{0:0}", price);
NumberFormatInfo format = new System.Globalization.NumberFormatInfo();
format.CurrencyDecimalDigits = 2;
format.CurrencyDecimalSeparator = ",";
format.CurrencyGroupSeparator = "";
return decimal.Parse(price).ToString(format);
var input = "1,200.99";
//Convert to decimal using US culture (or other culture using . as decimal separator)
decimal value = decimal.Parse(input, CultureInfo.GetCultureInfo("en-US"));
//Convert to string using DE culture (or other culture using , as decimal separator)
string output = value.ToString(CultureInfo.GetCultureInfo("de-DE"));
Console.WriteLine(output); //1200,99
What about something like this:
double number;
double.TryParse("1,200.99", NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-US"), out number);
var formattedNumber = number.ToString(CultureInfo.CreateSpecificCulture("de-DE"));
Then return or write out formattedNumber (whatever you need to do).
Yes and no. First, what you have is a string, and so you cannot change the formatting of it as you're attempting to. However, to achieve what you would like, you can parse the string into a decimal value and then use the formatting options for decimals to display it in any reasonable way.
You may try for something like this:
String.Format("{0:#,###0}", 0);
or may be like this:
string str = yourNumber.Remove(",").Replace(".",",");
Close enough tronc,
Try this snippet:
String curStr = "12.95";
Decimal decVal;
var valid = Decimal.TryParse(curStr, out decVal);
if (!valid) throw new Exception("Invalid format.");
String newFormat = decVal.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"));
Within the toString(...) call, you can append a number after 'C' to specify how many decimal places should follow. E.g "C3".
So I'm learning and practicing WP7 application development.
I'm working with integers (currency), and it seems to always display four integers after the decimal place. I'm trying to cut it down to just either ONE or TWO decimal places.
I've been trying to use the "my variable.ToString("C2")" (C for Currency, 2 for number of ints after the decimal)
I'm probably missing something obvious, but please help
decimal number = new decimal(1000.12345678);
string text = number.ToString("#.##");
Output:
1000,12
An other way:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalDigits = 2;
decimal val = new decimal(1000.12345678);
string text = val.ToString("c", nfi);
When formatting a currency, NumberFormatInfo allows specifying following properties as well:
CurrencyDecimalDigits
CurrencyDecimalSeparator
CurrencyGroupSeparator
CurrencyGroupSizes
CurrencyNegativePattern
CurrencyPositivePattern
CurrencySymbol
See Custom Numeric Format Strings on MSDN for more examples
The "C" format string defines the currency specifier as described on MSDN. This will include the currency symbol for the current culture, or for a specific culture if supplied, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("C", CultureInfo.CreateSpecificCulture("en-US"));
// This gives $1234.56
In your case, it seems that you have a limited set of currency symbols that you support, so I would suggest using the fixed point format specifier "F" instead. By default this will give you 2 decimal points, but you can specify a number to vary this, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("F");
// This gives 1234.56
formatted = amount.ToString("F3");
// This gives 1234.567
Using the fixed point specifier will give you control over the number of decimal points and enable you to concatenate the currency symbol.
The only thing I would add to "sll" answer is to pay attention on Culture (they often forget to mantion this), like this (example)
string text = val.ToString("#.##", CultureInfo.InvariantCulture);
double total = 526.4134
string moneyValue = total.ToString("c");
This will display it in this format: $#.##