Handling decimal separators (comma and dot) for a whole program with few lines of code

ng80092b

I'm trying to make my program more compatible, for that I've ended up changing a lot of little things for example,

Using textBox.Text = Convert.ToString(value) instead of = "value"

Getting the current user decimal separator and using it replace on a tryparse

char sepdec = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

float.TryParse(str.Replace(",", sepdec.ToString()).Replace(".", sepdec.ToString()), out testvariable;

But these solutions are hard to implement when you have already coded most of your program without worrying about it.

So I'm trying to find ways to make the whole code compatible, without having to edit every tryparse and every textbox

I've tried to do the following:

//Get the current user decimal separator before the program initializes
char sepdec = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

//Create a current culture clone and change the separator to whatever the user has in his regional options, before the initializing the component

  public Form1()
  {
  System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  customCulture.NumberFormat.NumberDecimalSeparator = sepdec.ToString();

  System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

  InitializeComponent();
  }

But I've tested this, and it's not really doing anything. Wasn't it supposed to make the program understand something like ok, now you use dot as your decimal separator altough you have values in textBox as "2,5" ?

Soner Gönül

ok, now you use dot as your decimal separator altough you have values in textBox as "2,5"

Exactly.

float.TryParse method uses your CurrentCulture settings if you don't use any IFormatProvider with it.

If you try to parse "2,5" to float without any IFormatProvider, your CurrentCulture has to have , as a NumberDecimalSeparator.

If you try to parse "2.5" to float, you either use a culture as an another parameter which already has . as a NumberDecimalSeparator (like InvariantCulture), or you can .Clone() your CurrentCulture (as you did) and set it's NumberDecimalSeparator property to . and use this cloned culture as an another parameter in float.TryParse overload.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Format a number with comma separators and round to 2 decimal places in Python 2?

From Dev

AngularJS decimal input: change comma to dot

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Difference between decimal number with comma and dot Javascript

From Dev

Allow dot and comma in numbers, not only for decimal

From Dev

String to decimal conversion: dot separation instead of comma

From Dev

Number input replacing decimal comma with dot

From Dev

Regex for decimal number dot instead of comma (.NET)

From Dev

Regex for formatting comma for decimal places and dot for thousands

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Regular Expression for Whole Number (Thousands comma, No decimal)

From Dev

C# aspx decimal thousand with comma and decimal with dot

From Dev

Handling a keyboard event in one place for the whole program

From Dev

How to accept both dot and comma as a decimal separator with WTForms?

From Dev

If decimal value, convert to two decimals AND dot separated value to comma separated

From Dev

Decimal pad can't do math with comma. Need dot

From Dev

Magento: Decimal Price in spanish language display Dot instead comma

From Dev

Formatting doubles to two decimal places in Java produces a comma instead of a dot

From Dev

Values over plot– one decimal place, a comma instead of a dot

From Dev

Replace the decimal dot with a comma on an axis in D3?

From Dev

How to convert floating point decimal separator from dot to comma in Javascript

From Dev

How to change the decimal separator from a comma to a dot in SQL Server

From Dev

Program prints individual lines and not whole file

From Dev

Handling excessive path separators

From Dev

Replace ,(comma) by .(dot) and .(dot) by ,(comma)

From Dev

How to benchmark few lines of code in nim?

From Dev

Translate a few assembly lines into C-code

From Dev

Increasing datetime with days in few lines of code

From Dev

Translate a few assembly lines into C-code

Related Related

  1. 1

    Format a number with comma separators and round to 2 decimal places in Python 2?

  2. 2

    AngularJS decimal input: change comma to dot

  3. 3

    NumericUpDown: accept both comma and dot as decimal separator

  4. 4

    Difference between decimal number with comma and dot Javascript

  5. 5

    Allow dot and comma in numbers, not only for decimal

  6. 6

    String to decimal conversion: dot separation instead of comma

  7. 7

    Number input replacing decimal comma with dot

  8. 8

    Regex for decimal number dot instead of comma (.NET)

  9. 9

    Regex for formatting comma for decimal places and dot for thousands

  10. 10

    NumericUpDown: accept both comma and dot as decimal separator

  11. 11

    Regular Expression for Whole Number (Thousands comma, No decimal)

  12. 12

    C# aspx decimal thousand with comma and decimal with dot

  13. 13

    Handling a keyboard event in one place for the whole program

  14. 14

    How to accept both dot and comma as a decimal separator with WTForms?

  15. 15

    If decimal value, convert to two decimals AND dot separated value to comma separated

  16. 16

    Decimal pad can't do math with comma. Need dot

  17. 17

    Magento: Decimal Price in spanish language display Dot instead comma

  18. 18

    Formatting doubles to two decimal places in Java produces a comma instead of a dot

  19. 19

    Values over plot– one decimal place, a comma instead of a dot

  20. 20

    Replace the decimal dot with a comma on an axis in D3?

  21. 21

    How to convert floating point decimal separator from dot to comma in Javascript

  22. 22

    How to change the decimal separator from a comma to a dot in SQL Server

  23. 23

    Program prints individual lines and not whole file

  24. 24

    Handling excessive path separators

  25. 25

    Replace ,(comma) by .(dot) and .(dot) by ,(comma)

  26. 26

    How to benchmark few lines of code in nim?

  27. 27

    Translate a few assembly lines into C-code

  28. 28

    Increasing datetime with days in few lines of code

  29. 29

    Translate a few assembly lines into C-code

HotTag

Archive