简体   繁体   中英

How to prevent decimal.TryParse convert “,” to “.”?

This is my code:

string myValue = "0,203";

decimal.TryParse(myValue, NumberStyles.Any, CultureInfo.CurrentCulture, out myValueAsDecimal;

...

myValueAsDecimal is 0.203 now

Is it possible that myValueAsDecimal has 0,203 after TryParse or the internal representation of decimal is always 0.203 and I need to format GUI output if I need 0,203?

Is it possible that myValueAsDecimal has 0,203 after TryParse

No. It's just a number - it has no concept of a text format. To think about it another way with a simpler type, consider these two lines of code:

int x = 0x100;
int y = 256;

Are those two values the same? Yes, they represent the same number. If you convert the values of x and y to strings, by default they will both end up as "256" - but they could both end up as "100" if you request a hex representation.

It's important to distinguish between the real value of a variable and a textual representation. Very few types (none that I can think of immediately) carry around information about a textual representation with them - so for example, a DateTime can be parsed from a variety of formats, but has no "memory" of an original text format. It's just a date and time, which could then be formatted according to any format.

If you need to maintain the idea of "a decimal number and the culture in which it was originally represented" then you should create your own class or struct for that pairing. It's not present in decimal itself.

decimal d = 0.203m;

Console.WriteLine(d.ToString(CultureInfo.InstalledUICulture));
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));        // decimal point: dot
Console.WriteLine(d.ToString(CultureInfo.GetCultureInfo("en-US"))); // default decimal point: dot
Console.WriteLine(d.ToString(CultureInfo.GetCultureInfo("ru-RU"))); // default decimal point: comma

Result:

0,203
0.203
0.203
0,203

Looks like your CurrentCulture has , as a NumberDecimalSeparator and that's why your parsing succeed.

Actually , 0.203 and 0,203 are the same as value. Only matter is their textual representation when you print it.

If you wanna get your value as a 0,203 representation, you can use a culture that has , as a NumberDecimalSeparator .

For example, my culture ( tr-TR ) has a , . When you represent your decimal with it, you will get 0,203 .

string myValue = "0,203";
decimal myValueAsDecimal;
decimal.TryParse(myValue, NumberStyles.Any, CultureInfo.CurrentCulture, out myValueAsDecimal);
myValueAsDecimal.ToString(new CultureInfo("tr-TR")).Dump(); // 0,203

The value of the Decimal is the same regardless of the Culture, it's

0.203

what changes is its String representation (decimal separator in your case), so if you want to change decimal separator and don't want to change the Culture you can just assign NumberDecimalSeparator in your custom NumberFormatInfo eg

  Decimal d = 0.203M;

  NumberFormatInfo myNumberInfo = new NumberFormatInfo() {
    NumberDecimalSeparator = "," // Comma, please
  };

  String result = d.ToString(myNumberInfo); // "0,203"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM