Formatting currency in .NET - c#

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.

Related

Data showing in Exponential format

My data 0.00007173535 is showing as 7.1735351E-05 in aspx page.
My required format is decimal even the number has 20 decimal points.
I know its a small thing but couldn't figure it out.
My code is below that is in a repeater.
<%# Math.Round(Convert.ToDouble(Eval("Ranking.Rating")),12)%>
Or you can use "F" format specifier as well.
yourValue.ToString("F20");
Just call ToString(format) on it:
0.00007173535.ToString("0.0#################")
outputs
0.00007173535
or
0.00007173535.ToString("N12")
outputs
0.000071735350
See MSDN: Custom Numeric Format Strings for more information.
In your case this would be:
Convert.ToDouble(Eval("Ranking.Rating")).ToString("N12")

Currency localization and handling in C#

I am looking for a strategy to implement different currencies in my system.
For GB I use pounds locale and it works ok with numbers like "7.55", but as soon I change locale to Spanish for example I get the "7,55" number and I cannot handle it.
So the first thing is that I calculate some amount with JavaScript client side and JavaScript cannot calculate comma numbers, nor I can parseFloat, nor converting to decimal number if I don't replace commas with numbers like:
input.val().replace(',', '.');
I mean I don't understand how to handle this properly in scenarios when you have to make payment. When to convert for calculations, etc.
Is there any proper way to do this in .net with JavaScript help? Just need some tips.
In c#, you can use CultureInfo.InvariantCulture for this. You have a full snippet about currency here.
string str = "42.5";
float numFloat = float.Parse(str, CultureInfo.InvariantCulture);
Learn more from the msdn.

C# - Force String.Format to use decimals and NEVER commas

Basically I'm realizing that my application is using commas instead of decimals, and i NEVER want to allow this. Anyone know how I can correct? I can't find one thing via google that is to force decimals, it's all about forcing commas.
return String.Format("{0}f, {1}f, {2}f, {3}f, {4}f, {5}f, {6}f, {7}f, {8}f, {9}f, {10}f, {11}f, {12}f, {13}f, {14}f, {15}f", M.M11, M.M12, M.M13, M.M14, M.M21, M.M22, M.M23, M.M24, M.M31, M.M32, M.M33, M.M34, M.OffsetX, M.OffsetY, M.OffsetZ, M.M44);
You can use the other overload:
return String.Format(
CultureInfo.InvariantCulture // <<== That's the magic
, "{0}f, {1}f, {2}f, {3}f, {4}f, {5}f, {6}f, {7}f, {8}f, {9}f, {10}f, {11}f, {12}f, {13}f, {14}f, {15}f"
, M.M11, M.M12, M.M13, M.M14, M.M21, M.M22, M.M23, M.M24, M.M31, M.M32, M.M33, M.M34, M.OffsetX, M.OffsetY, M.OffsetZ, M.M44
);
This way of calling ensures that the invariant culture is being passed as the format provider to the String.Format, ensuring that you get dots for numbers, dollars for currency symbols, English for names of the months and days, and so on.
Try setting the culture to US English for the String.Format function:
String.Format(new CultureInfo("en-US"), "{0}f, {1}f, {2}f", etc)

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

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/

c#: how to force trailing zero in numeric format string?

I need to display float as
1.00
1.50
1.55
1.60
The following is what I see using f2 format.
1.
1.5
1.55
1.6
Is there a way to force the trailing 0 to appear?
(I'm using a DevExpress SpinEdit control and trying to set the display and edit format.)
yourNumber.ToString("N2");
You can use syntax like this:
String.Format("{0:0.00}", n)
For future reference,
http://www.csharp-examples.net/string-format-double/
On those rare occasions I need to formatting, I go here:
http://blog.stevex.net/index.php/string-formatting-in-csharp/
spinEdit.Properties.DisplayFormat.FormatType = FormatType.Numeric;
spinEdit.Properties.DisplayFormat.FormatString = "C2";
In the future, though, I would recommended searching Dev Express' knowledge base or emailing support (support#devexpress.com). They'll be able to answer your question in about a day.
You can also do this with string interpolation (note that this is C# 6 and above):
double num = 98765.4;
Console.WriteLine($"{num:0.00}"); //Replace "0.00" with "N2" if you want thousands separators
Let's say you had a variable called "myNumber" of type double:
double myNumber = 1520;
Instead of:
myNumber.ToString("N2"); // returns "1,520.00"
I would opt for:
myNumber.ToString("0.00"); // returns "1520.00"
since N2 will add "," thousands separators, which can often cause problems downstream.

Categories