tags:

views:

467

answers:

8

How can I change values in string from 0,00 to 0.00? - only numeric values, not all chars "," to "."

FROM

string myInputString = "<?xml version=\"1.0\"?>\n<List xmlns:Table=\"urn:www.navision.com/Formats/Table\"><Row><HostelMST>12,0000</HostelMST><PublicMST>0,0000</PublicMST><TaxiMST>0,0000</TaxiMST><ParkMST>0,0000</ParkMST><RoadMST>0,0000</RoadMST><FoodMST>0,0000</FoodMST><ErrorCode>0</ErrorCode><ErrorDescription></ErrorDescription></Row></List>\n";

TO

string myInputString = "<?xml version=\"1.0\"?>\n<List xmlns:Table=\"urn:www.navision.com/Formats/Table\"><Row><HostelMST>12.0000</HostelMST><PublicMST>0.0000</PublicMST><TaxiMST>0.0000</TaxiMST><ParkMST>0.0000</ParkMST><RoadMST>0.0000</RoadMST><FoodMST>0.0000</FoodMST><ErrorCode>0</ErrorCode><ErrorDescription></ErrorDescription></Row></List>\n";
+1  A: 
string newStr = myInputString.Replace("0,00", "0.00");
flq
You're missing a " and I believe the OP wanted a solution for numbers on the form 0.00 and not just 0.00 specifically.
Brian Rasmussen
A: 

if the , is not used anywhere else but number with in the string you can use the following:
string newStr = myInputString.Replace(",", ".");

Bashar Kokash
+5  A: 

Try this :

Regex.Replace("attrib1='12,34' attrib2='43,22'", "(\\d),(\\d)", "$1.$2")

output : attrib1='12.34' attrib2='43.22'

Canavar
asda and denem are some pretty cool metasyntactic names!
A: 

Thanks for answers, but I mean to change only numeric values, not all chars "," to "." I don't want change string from

string = "<Attrib>txt txt, txt</Attrib><Attrib1>12,1223</Attrib1>";

to

string = "<Attrib>txt txt. txt</Attrib><Attrib1>12.1223</Attrib1>";

but this one is ok

string = "<Attrib>txt txt, txt</Attrib><Attrib1>12.1223</Attrib1>";
+1  A: 

The answer from ScarletGarden is a start, but you'll need to know the complete context and grammar of "numeric values" in your data.

The problem with the short answer is that cases such as this get modified:

<elem1>quantity<elem2>12,6 of which were broken</elem2></elem1>

Yes, there's probably a typo (missing space after the comma) but human-entered data often has such errors.

If you include more context, you're likely to reduce the false positives. A pattern like

([\s>]-?$?\d+),(\d+[\s<])

(which you can escape to taste for your programming language of choice) would only match when the "digits-comma-digits" portion (with optional sign and currency symbol) was bounded by space or an end of an element. If all of your numeric values are isolated within XML elements, then you'll have an easier time.

joel.neely
+4  A: 

The best method depends on the context. Are you parsing the XML? Are you writing the XML. Either way it's all to do with culture.

If you are writing it then I am assuming your culture is set to something which uses commas as decimal seperators and you're not aware of that fact. Firstly go change your culture in Windows settings to something which better fits your culture and the way you do things. Secondly, if you were writing the numbers out for human display then I would leave it as culturally sensative so it will fit whoever is reading it. If it is to be parsed by another machine then you can use the Invariant Culture like so:

12.1223.ToString(CultureInfo.InvariantCulture);

If you are reading (which I assume is what you are doing) then you can use the culture info again. If it was from a human source (e.g. they typed it in a box) then again use their default culture info (default in float.Parse). If it is from a computer then use InvariantCulture again:

float f = float.Parse("12.1223", CultureInfo.InvariantCulture);

Of course, this assumes that the text was written with an invariant culutre. But as you're asking the question it's not (unless you have control over it being written, in which case use InvariantCulture to write it was suggested above). You can then use a specific culture which does understand commas to parse it:

NumberFormatInfo commaNumberFormatInfo = new NumberFormatInfo();
commaNumberFormatInfo.NumberDecimalSeperator = ",";
float f = float.Parse("12,1223", commaNumberFormatInfo);
ICR
Actually I upvoted your answer because I didn't know about NumberFormatInfo. I wrote about it because when I was finishing my answer, yours came up and I readed it before posting mine. I used CultureInfo before knowing about NumberFormatInfo.
Jader Dias
+2  A: 

I strongly recommend joel.neely's regex approach or the one below:

  1. Use XmlReader to read all nodes
  2. Use double.TryParse with the formatter = a NumberFormatInfo that uses a comma as decimal separator, to identify numbers
  3. Use XmlWriter to write a new XML
  4. Use CultureInfo.InvariantCulture to write the numbers on that XML
Jader Dias
This basicaly explains my suggestion but a lot more consisely and cleanly :P
ICR
A: 

While you could theoretically do this using a Regex, the pattern would be complex and hard to to test. ICR is on the right track, you need to do this based on culture.

Do you know that your numbers are always going to be using a comma as a decimal separator instead of a period? It looks like you can, given that Navision is a Danish company.

If so, you'll need to traverse the XML document in the string, and rewrite the numeric values. It appears you can determine this on node name, so this won't be an issue.

When you convert the number, use something similar to this:

here's what you want to do:

    internal double ConvertNavisionNumber(string rawValue)
    {
        double result = 0;

        if (double.TryParse(rawValue, NumberStyles.Number, new CultureInfo("da-DK").NumberFormat, out result))
            return result;
        else
            return 0;
    }

This tells the TryParse() method that you're converting a number from Danish (da-DK). Once you call the function, you can use ToString() to write the number out in your local format (which I'm assuming is US or Canadian) to get a period for your decimal separator. This will also take into account numbers with different thousands digit separator (1,234.56 in Canada is written as 1 234,56 in Denmark).

ConvertNavisionNumber("4,43").ToString()

will result in "4.43".

ConvertNavisionNumber("1 234").ToString()

will result in "1,234".

Dan R