How to convert price by format
I have encountered开发者_开发技巧 with a problem with pricing. I need to format price input to be of the type XXXX.YY the problem is, the input price can be of shape XXX,YY in europe or XX,XXX.YY if talking about big numbers.
Is there a JS or C# lib that helps there?
thanks
You should use Decimal.Parse rather than Double.Parse when dealing with currency values. (The Decimal type reduces the possibility of rounding errors etc.)
To answer your question about differing cultural currency formatting, from MDSN:
Parameter s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, use the Decimal.Parse(String, IFormatProvider) or Decimal.Parse(String, NumberStyles, IFormatProvider) method.
In case you are not aware, the .NET framework automatically takes the "current system culture" from the current regional settings of the operating system. In Windows this can be viewed/changed by the computer user in the "Regional Settings" or similar.
for American / British format:
Double.Parse("123,456.78", new CultureInfo("en-US"));
for German format:
Double.Parse("123.456,78", new CultureInfo("de-DE"));
Hint: If you are storing / reading data from file/Database etc. it is generally advisable to make use of CultureInfo.InvariantCulture
Double.Parse("123,456.78")
will work in C#
http://msdn.microsoft.com/en-us/library/7yd1h1be.aspx
Then ToString it to the format you want:
String.Format("£{0:##.##}", number);
精彩评论