views:

43

answers:

3

Hey

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

A: 
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);
Andrew Bullock
+1  A: 

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

yas4891
If you are storing it in a database, send the date as a DateTime value in a parameter.
Cylon Cat
@Cylon Cat: This is about doubles, not dates
yas4891
+2  A: 

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.

Ash
Good hint. Didn't know that
yas4891
my first comment was about the fact, that Decimal.Parse does round instead of truncate
yas4891