How to change symbol for decimal point in double.ToString()?
Some shortcut is to create a NumberFormatInfo class, set its NumberDecimalSeparator property to "." and use the class as parameter to ToString() method whenever u need it.
using System.Globalization;
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ".";
value.ToString(nfi);
Converting double to string with N decimals, dot as decimal separator, and no thousand separator
For a decimal
, use the ToString method, and specify the Invariant culture to get a period as decimal separator:
value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
The long
type is an integer, so there is no fraction part. You can just format it into a string and add some zeros afterwards:
value.ToString() + ".00"
C# Double - ToString() formatting with two decimal places but no rounding
I use the following:
double x = Math.Truncate(myDoubleValue * 100) / 100;
For instance:
If the number is 50.947563 and you use the following, the following will happen:
- Math.Truncate(50.947563 * 100) / 100;
- Math.Truncate(5094.7563) / 100;
- 5094 / 100
- 50.94
And there's your answer truncated, now to format the string simply do the following:
string s = string.Format("{0:N2}%", x); // No fear of rounding and takes the default number format
Double ToString gives rounded result
See: MSDN for the standard ToString
conversion:
If format is null or an empty string, the return value is formatted with the general numeric format specifier ("G").
This will show you, that the defaulted number format specifier "G", will default to max 15 digits if possible: G-format specifier
As requested:
double bigNum = 99999999999999.99;
Console.WriteLine(bigNum); //default
//100000000000000
Console.WriteLine(bigNum.ToString("G")); //explicit default
//100000000000000
Console.WriteLine(bigNum.ToString("G17")); //G17
//99999999999999.984
How do I display a decimal value to 2 decimal places?
decimalVar.ToString("#.##"); // returns ".5" when decimalVar == 0.5m
or
decimalVar.ToString("0.##"); // returns "0.5" when decimalVar == 0.5m
or
decimalVar.ToString("0.00"); // returns "0.50" when decimalVar == 0.5m
String to decimal conversion: dot separation instead of comma
Thanks for all reply.
Because I have to write a decimal number in a xml file I have find out the problem. In this discussion I have learned that xml file standard use dot for decimal value and this is culture independent.
So my solution is write dot decimal number in a xml file and convert the readed string from the same xml file mystring.Replace(".", ",");
Thanks Agat for suggestion to research the problem in xml context and Ε Г И І И О because I didn't know visual studio doesn't respect the culture settings I have in my code.
Formatting a double to two decimal places
string.Format
will not change the original value, but it will return a formatted string. For example:
Console.WriteLine("Earnings this week: {0:0.00}", answer);
Note: Console.WriteLine
allows inline string formatting. The above is equivalent to:
Console.WriteLine("Earnings this week: " + string.Format("{0:0.00}", answer));
Language invariant Double.ToString()
Use:
string networkMsg = "command " + value.ToString(CultureInfo.InvariantCulture);
or:
string networkMsg = string.Format(CultureInfo.InvariantCulture, "command {0}", value);
This needs using System.Globalization;
in the top of your file.
Note: If you need full precision, so that you can restore the exact double again, use the Format
solution with the roundtrip format {0:R}
, instead of just {0}
. You can use other format strings, for example {0:N4}
will insert thousands separators and round to four dicimals (four digits after the decimal point).
Since C# 6.0 (2015), you can now use:
string networkMsg = FormattableString.Invariant($"command {value}");
Parse strings to double with comma and point
You want to treat dot (.
) like comma (,
). So, replace
if (double.TryParse(values[i, j], out tmp))
with
if (double.TryParse(values[i, j].Replace('.', ','), out tmp))
Related Topics
Why Does Enumerable.All Return True for an Empty Sequence
Getting/Setting File Owner in C#
Get Os Version/Friendly Name in C#
Double Buffering When Not Drawing in Onpaint(): Why Doesn't It Work
The Provided Uri Scheme 'Https' Is Invalid; Expected 'Http'. Parameter Name: Via
Simplest Way to Do a Fire and Forget Method in C# 4.0
Export Datatable to Excel File
How to Remove All White Space from the Beginning or End of a String
Error in Process.Start() -- the System Cannot Find the File Specified
Find Index of a Value in an Array
How to Invoke a UI Method from Another Thread
Play and Wait for Animation/Animator to Finish Playing
Appending an Existing Xml File with Xmlwriter
Does Entity Framework Code First Support Stored Procedures
Large Switch Statements: Bad Oop