How to convert double to string without the power to 10 representation (E-05)
double value = 0.000099999999833333343;
string text = value.ToString();
Console.WriteLine(text); // 9,99999998333333E-05
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
I've tried the following variants
Console.WriteLine(value.ToString()); // 9,99999998333333E-05
Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05
Console.WriteLine(value.ToString("N20")); // 0,00009999999983333330
Console.WriteLine(String.Format("{0:F20}", value)); // 0,00009999999983333330
Doing tostring N20 or format F20 seems closest to what I want, but I do end up with a lot of trailing zeros, is there a clever way to avoid this? I'd like to get as close to the double representation as possible 0.000099999999833333343
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
How about
Convert.ToDecimal(doubleValue).ToString()
You don't need string.Format(). Just put the right format string in the existing .ToString() method. Something like "N" should do.
Use string.Format with an appropriate format specifier.
This blog post has a lot of examples: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx
Related
I have been working on a project, and found an interesting problem:
2.ToString("TE"+"000"); // output = TE000
2.ToString("TR"+"000"); // output = TR002
I also have tried with several strings other than "TE" but all have the same correct output.
Out of curiosity, I am wondering how come this could have happened?
Simply based on Microsoft's documentation, Custom Numeric Format Strings, your strings "TE000" and "TR000" are both custom format strings, but clearly they are parsed differently.
2.ToString("TE000") is just a bug in the formatter; it's going down a buggy path because of the unescaped "E". So it's unexpectedly assuming the whole thing is a literal.
2.ToString("TR000") is being interpreted as an implied "TR" literal plus 3 zero-filled digits for an integer value; therefore, you get "TR002".
If you truly want TE and TR verbatim, the expressions 2.ToString("\"TE\"000") and 2.ToString("\"TR\"000") will accomplish that for you by specifying TE and TR as explicit literals, instead of letting the formatter guess if they are valid format specifiers (and getting it wrong).
The ToString needs to PARSE the format string and understand what to do with it.
Let's take a look to the following examples:
2.ToString("TE000"); //output TE000
2.ToString("E000"); //output 2E+000
2.ToString("0TE000); //output 2TE000
2.ToString("T"); //throws exception
2.ToString("TT"); //output TT
This shows that if the ToString parser can understand at least part of the format, it will assume that the rest is just extra characters to print with it. If the format is invalid for the given number (like when you use a DateTime string format on a number), it will throw an exception. If it can not make sense of the format, it will return the format string itself as the result.
You cannot use a numeric format to achieve a custom format, instead use something like this:
int i = 2;
String.Format("TE{0:X3}", i);
See Custom Numeric Format Strings. The E means the exponent part of the scientific notation of the number. Since 2 is 2E000 in exponential notation, that might explain it.
I have some strings such as:
1.5555555555555
2.3421354325435354545
4.509019292
I want to format them into a string such as:
1.5555
2.3421
4.5090
I tried to use the C# String.Format but I can not get it to correctly work.
Can someone please give me the correct c# statement to accomplish this?
Thanks.
It's unclear if you'll always be dealing with numeric values. If you want to avoid parsing the strings as numbers, you might try something like this:
public static string TrimTo(string str, int maxLength)
{
if (str.Length <= maxLength)
{
return str;
}
return str.Substring(0, maxLength);
}
This will trim the provided string to six characters, if it's longer than six. This seems to be what you want, but (as Kees points out), will do something unexpected with a string like "1234567.890".
The conditional clause is necessary here because String.Substring will complain if the second index is outside of the string (if the string is shorter than maxLength, in other words).
(If you've played around with C# 3.0 extension methods at all, you might recognize this, slightly modified from the above, as an excellent opportunity for one: string trimmed = s.TrimTo(10);)
string.Format("{0:N4}",decimalValue);
Standard Numeric Format Strings
Custom Numeric Format Strings
If you convert the Strings to doubles you can use String.Format to specify how many decimal places you want to include when you reformat it as a String.
String.Format("{0:0.0000}", double.Parse("1.55555555555555"))
I am trying to format a double in C# such that it uses the thousand separator, and adds digits upto 4 decimal places.
This is straight forward except that I dont want to have the decimal point if it is an integer. Is there a way to do this using the custom numeric format strings rather than an if statement of tenary operator?
Currently I have:
string output = dbl.ToString(dbl == (int)dbl ? "#,##0" : "#,##0.####");
Thanks
I believe your second format string of "#,##0.##" should be exactly what you want -- the # format character is a placeholder that will NOT display zeros.
If you had "#,###.00" then you would get trailing zeros.
test code:
double d = 45.00;
Console.Writeline(d.ToString("#,##0.##"));
Gives output of "45". Setting d to 45.45 gives output "45.45", which sounds like what you're after.
So you had the answer after all! ;)
Incidentally, there's a handy cheat-sheet for format strings (amongst other handy cheat-sheets) at http://john-sheehan.com/blog/net-cheat-sheets/
No, there is not any built-in format string for this. Your current solution is the best way to accomplish this.
MSDN lists both the standard numeric format strings and custom numeric format strings, so you should be able to see for yourself that none directly matches your needs.
How to convert double to string without the power to 10 representation (E-05)
double value = 0.000099999999833333343;
string text = value.ToString();
Console.WriteLine(text); // 9,99999998333333E-05
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
I've tried the following variants
Console.WriteLine(value.ToString()); // 9,99999998333333E-05
Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05
Console.WriteLine(value.ToString("N20")); // 0,00009999999983333330
Console.WriteLine(String.Format("{0:F20}", value)); // 0,00009999999983333330
Doing tostring N20 or format F20 seems closest to what I want, but I do end up with a lot of trailing zeros, is there a clever way to avoid this? I'd like to get as close to the double representation as possible 0.000099999999833333343
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
How about
Convert.ToDecimal(doubleValue).ToString()
You don't need string.Format(). Just put the right format string in the existing .ToString() method. Something like "N" should do.
Use string.Format with an appropriate format specifier.
This blog post has a lot of examples: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx
After I convert a decimal value salePr to string, using following code:
decimal salePr;
string salePrStr;
...
salePrStr = (salePr).ToString("0000.00");
'''
I'd like to get rid of leading zeros (in case result is <1000).
What is right and the best way to do this operation?
So why have you explicitly included them? Just use a format string of 0.00.
You could use trimstart to remove the leading zeros.
salePrStr = (salePr).ToString("0000.00").TrimStart(Convert.ToChar("0"));
It looks like you are trying to display currency, if you want to display it as currency, try salePrStr = String.Format("{0:C}", salePr) otherwise use the format 0.00
salePrStr = (salePr).ToString("###0.00");
The other answers are probably what you're looking for. If, for some reason, however, you actually want to keep the original strings (with leading zeroes), you can then write:
string salePrStr = salePr.ToString("0000.00");
string salePrStrShort = salePrStr.TrimStart('0');
Give this a try:
salePrStr = (salePr).ToString("N2");
That would make 1000.10 show as
1,000.10
and make 45.2305 show as
45.23
Just testing it in c#