Click here to Skip to main content
15,391,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I wrote a simple program which allows to test the number in double type. The objective is to read an xml file according to the decimal symbol chosen by the user.

when the user changes the configuration of decimal symbol the program must work with this modification without changing each time the xml file.

so how do I process my code to make the value="1,2" acceptable even if the user changes their decimal symbol?

What I have tried:

C#
XDocument xdoc = XDocument.Parse(Properties.Resources.Number);
          string number = xdoc.Element("Number").Attribute("value").Value;

          double result;
          var isDouble = Double.TryParse(number, out result);
          if (!isDouble)
              Console.WriteLine("Error:Number is not a double");
          else
              Console.WriteLine("Congratulations: it's a Double number");


XML
<?xml version="1.0" encoding="utf-8"?>
<Number value="1,2" />
Posted
Updated 1-Mar-22 0:41am
Comments
englebart 11-Mar-22 20:10pm
   
This seems like a very hard problem to solve. You should probably require the name of the locale in addition to the number.

Is 1,000
- one thousand or
- one with accuracy expressed to the 3rd decimal position (thousandths)

It depends on your locale! Therefore locale is a required input.

It depends on how much "change" you allow the user to make.

A simple change of locale so that instead of a valid number being in UK format "ddd,ddd,ddd.ddd" but in German "ddd.ddd.ddd,ddd", or Indian "dd,dd,dd,ddd.ddd" is relatively simple: you specify the locale being used to parse the number: Double.TryParse Method (System) | Microsoft Docs[^]
But any "data specific" formatting such as "ddd#ddd#ddd/ddd" would require you to process the string manually and convert the number yourself.
   
Comments
Maciej Los 3-Mar-22 6:45am
   
5ed!
You'll need to specify a custom NumberFormatInfo instance:
C#
string number = "1,2";

var provider = new System.Globalization.NumberFormatInfo
{
    NumberDecimalSeparator = ",",
    NumberGroupSeparator = "",
};

bool isDouble = double.TryParse(number, System.Globalization.NumberStyles.Float, provider, out double result);
   
Comments
Maciej Los 3-Mar-22 6:45am
   
5ed!
To add to Richard Deemings answer, if I understand correctly, you don't know whether the specified string uses a comma or a dot as the decimal separator. I'm not sure if you work with thousand separators, but you could get 1.000,00 as well as 1,000.00 and both would be valid. This is a huge issue for me as I'm Dutch, where we use a comma as decimal separator, but many computers and keyboards have an international layout.

I use the following code, which basically checks the first instance of a dot or a comma. If a dot is the first one, then the comma is the decimal separator and if the comma is the first one, then the dot is the decimal separator (if one doesn't appear it's -1 and the other is the first one and if neither appear it doesn't really matter anyway).
Richard's code would then be something like (taken from production code):
C#
var value = "1.000,00"; // or 1,000.00

var indexOfDot = value.IndexOf('.');
var indexOfComma = value.IndexOf(',');
var numberFormat = new NumberFormatInfo
{
    NumberDecimalSeparator = indexOfComma > indexOfDot ? "," : ".",
    NumberGroupSeparator = indexOfComma > indexOfDot ? "." : ","
};
var success = decimal.TryParse(value, NumberStyles.Number, numberFormat, out var parsedValue);
if (success)
{
    // Use parsedValue, which now has your decimal.
}
else
{
    // The string is not a valid decimal.
}
   
Comments
Maciej Los 3-Mar-22 6:45am
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900