How to specify total number of character in C# number formatting? - c#

Is there a way to specify total number of characters when formatting doubles?
Lets say I have 0.00012345678, and I specify total number of characters (7), I want to get 1.23e-4. Format "G7" would give 1.2345e-4.
More examples:
0.00000012345678F -> 1.23e-7
0.00012345678F -> 1.23e-4
0.12345678F -> 1.23e-1
1.2345678F -> 1.23457
12.345678F -> 12.3457
12345678F -> 1.234e8

You probably want to use the "e" format string like so...
String.Format("{0:0.00e+0}", number);

You have a misunderstanding about the meaning of "precision". For a floating point number, "precision" means the number of significant digits, so the result returned by "G6" is correct.
If you want a fixed number of characters, use a custom format string like Jason suggested.

{0:00e+0}
http://blog.stevex.net/string-formatting-in-csharp/

Related

displaying decimal values depending on decimal places in string.format

I'm using String.Format for displaying validation messages. I'm trying to achieve a scenario where, if decimal is there, show 12.34, else don't show any decimal points like, 12.
I tried to achieve it by using type as number. My string is,
Please enter value between {1:N} and {2:N}. // Displays 1.00 and 2.00
Please enter value between {1:N0} and {2:N0}. // Displays 1 and 2
What I should do to fix this? I need comma separation depending on culture. Using {1:G} will not provide that.
Try using :G . for isntance: Please enter value between {1:G} and {2:G}. Or {1:0.##}
The 0 means a digit will always be shown. Use a # to show optional digits.
{1:0.##}

Combining C# String.Format format options

I'd like to format my number as a percent value with an always visible sign.
To format it as a percent value I can do:
string.Format("{0:P2}", 1.45);
For visible signs I'll do:
string.Format("{0:+#.##;-#.##;0}", 1.45);
Any way to combine the two?
You probably just want to add % to custom format:
string.Format("{0:+#.##%;-#.##%;0}", 1.45); // output +145%

C# MVC String Format Zero Values

I currently use this to format my numbers.
#string.Format("£{0:#,###,###.##}", 1000) outputs £1,000
However when I enter a zero value it does this:
#string.Format("£{0:#,###,###.##}", 0.0) outputs £
How do i make this output even when i enter zero values? e.g £0.0
Thanks
The # character means "only use a digit when you need to".
I suspect you want:
#string.Format("£{0:#,###,##0.##}", value)
However, it would generally be a better idea just to use:
#string.Format("{0:c}", value)
... and let the .NET framework do the right thing.
The first 0 is the placeholder, means the first parameter. 00 is an actual format.
For example it could be like this:
Console.WriteLine(string.Format("£{0:#,###,##0.##}", 0));

Formatting currency in .NET

I am formatting the currency using Tostring() method i m using following syntax
ToString('##.##') it is working perfectly but in case of round number it remove last 2 zero
like for 100 it does not show 100.00 is shows 100.
how can i format in that way means
input desired output
100 100.00
100.10 100.10
Try "##.00" instead.
That will force two digits after the decimal separator.
You can also use ToString("C") to use the culture specific format in Windows directly.
First google result.
String.Format("{0:C}", x.ToString());
http://www.howtogeek.com/howto/programming/format-a-string-as-currency-in-c/
You can use :
.ToString("C")
Hope it helps.
Also, if you don't want the currency sign ($ in the US) added that "C" gives, you can also use "F2", which is "fixed number with 2 decimal places". It also has the advantage of giving you a thousands separator when you results go over 1,000.00.
This might help. Might be more than you need, but it takes globalization into account which might be necessary. "C" is also a short-cut currency format string that might get you further along.

How to force a sign when formatting an Int in c#

I want to format an integer i (-100 < i < 100), such that:
-99 formats as "-99"
9 formats as "+09"
-1 formats as "-01"
0 formats as "+00"
i.ToString("00")
is close but does not add the + sign when the int is positive.
Is there any way to do this without explicit distinguishing between
i >= 0 and i < 0?
Try this:
i.ToString("+00;-00;+00");
When separated by a semicolon (;) the first section will apply to positive values, the second section will apply to negative values, the third section will apply to zero (0).
Note that the third section can be omitted if you want zero to be formatted the same way as positive numbers. The second section can also be omitted if you want negatives formatted the same as positives, but want a different format for zero.
Reference: MSDN Custom Numeric Format Strings: The ";" Section Separator
You might be able to do it with a format string like so..
i.ToString("+00;-00");
This would produce the following output..
2.ToString("+00;-00"); // +02
(-2).ToString("+00;-00"); // -02
0.ToString("+00;-00"); // +00
Take a look at the MSDN documentation for Custom Numeric Format Strings
Try something like this:
i.ToString("+00;-00");
Some examples:
Console.WriteLine((-99).ToString("+00;-00")); // -99
Console.WriteLine(9.ToString("+00;-00")); // +09
Console.WriteLine((-1).ToString("+00;-00")); // -01
Console.WriteLine((0).ToString("+00;-00")); // +00

Categories