0

Easy way to parse decimal to string with thousand's separator.

for Example :

i fetch a decimal from Database was 100000000.00 with field's type decimal(18,2) and i have a TextBox in my Form with name's "payTxt".

then

i want parse 100000000.00 to 100.000.000,00 for fill the "payTxt".

i don't know why every i set "payTxt.Text = "100000000.00" "

its become "100000000,00"

  • 1
    `CultureInfo.NumberFormat` it has something to do with this – kevintjuh93 Oct 01 '15 at 13:48
  • 3
    possible duplicate http://stackoverflow.com/questions/11415405/decimal-to-string-with-thousands-separators – M_Idrees Oct 01 '15 at 13:49
  • 3
    parsing would be turning a string into a decimal, this sounds like you want **formatting** – James Oct 01 '15 at 13:52
  • 1
    While I think it's better to use NumericUpDown for data entry, another thing you should know is that String.Format() and ToString() method works based on Culture, You can take a look at examples that I provided. You should know `ToString("N2")` may result in a string that you don't like. – Reza Aghaei Oct 01 '15 at 15:00

3 Answers3

0
payTxt.Text = YourVariable.ToString("N2");
mituw16
  • 5,126
  • 3
  • 23
  • 48
0

Its better to use a NumericUpDown control with ThousandSeparator set to true and DecimalPlaces set to 2, and Maximum set to the value that you need:

here is result of setting the value: 1234567.89

enter image description here

This way:

  • Thousand separator and decimal point will be shown even when data entry.
  • You can set minimum and maximum allowed values
  • The user can only enter valid number values
  • You can set the increment when user clicks on arrows or use arrow keys.

Another thing you should know is that String.Format() and ToString() method works based on Culture, for example:

//Shows value based on your Thread.CurrentThread.CurrentCulture
MessageBox.Show(String.Format("{0:N2}", 123456.78));

//Shows 123,456.78
MessageBox.Show(String.Format(CultureInfo.GetCultureInfo("en"), "{0:N2}", 123456.78));

//Shows 123,456/78
MessageBox.Show(String.Format(CultureInfo.GetCultureInfo("fa"), "{0:N2}", 123456.78));

//Shows result based on your Thread.CurrentThread.CurrentCulture
MessageBox.Show((123456.78).ToString("N2"));

//Shows 123,456.78
MessageBox.Show((123456.78).ToString("N2", CultureInfo.GetCultureInfo("en")));

//Shows 123,456/78
MessageBox.Show((123456.78).ToString("N2", CultureInfo.GetCultureInfo("fa")));
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

to get 100.000.000,00 try the following:

Decimal dec = 100000000.00M;
CultureInfo ci = new CultureInfo("el-GR"); 
payTxt.Text = dec.ToString("0,0.00", ci);

for more info look on the following link: Custom Numeric Format Strings

"el-GR" is actually the greek CultureInfo, most other cultures will show a comma for the thousand digit separators.

martijn
  • 1,417
  • 1
  • 16
  • 26