How to change values ​​in a row from 0.00 to 0.00

How to change values ​​in a line from 0.00 to 0.00? - only numeric values, not all characters "," to "."

WITH

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"; 

Thanks for the answers, but I want to change only the numeric values, not all the characters "," to "." I do not want the 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 it normal

 string = "<Attrib>txt txt, txt</Attrib><Attrib1>12.1223</Attrib1>"; 
+4
source share
7 answers

I highly recommend joel.neely regex or one approach below:

  • Use XmlReader to read all nodes
  • Use double.TryParse with formatter = a NumberFormatInfo, which uses a comma as a decimal separator, to identify numbers
  • Use XmlWriter to write new XML
  • Use CultureInfo.InvariantCulture to write numbers on this XML
+2
source

Try the following:

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

output: attrib1 = '12 .34 'attrib2 = '43 .22'

+5
source

The best method depends on the context. Do you parse XML? You are writing XML. In any case, all this is connected with culture.

If you write this, I assume your culture is set to use commas as decimal separators, and you are not aware of this fact. First, change your culture in your Windows settings to something that best suits your culture and how you do it. Secondly, if you were writing numbers for the human display, I would leave it as culturally sensual, so that it matches the person who reads it. If it needs to be analyzed by another machine, you can use Invariant Culture like this:

 12.1223.ToString(CultureInfo.InvariantCulture); 

If you read (I suppose this is what you are doing), you can reuse cultural information. If it was from a human source (for example, they typed it in a box), then use your default culture information again (by 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 suggests that the text was written using an invariant cooler. But since you are asking the question in the wrong way (if you do not control it, then in this case, using InvariantCulture for writing was suggested above). Then you can use a specific culture that understands commas to analyze it:

 NumberFormatInfo commaNumberFormatInfo = new NumberFormatInfo(); commaNumberFormatInfo.NumberDecimalSeperator = ","; float f = float.Parse("12,1223", commaNumberFormatInfo); 
+4
source
 string newStr = myInputString.Replace("0,00", "0.00"); 
+1
source

The answer from ScarletGarden is the beginning, but you need to know the full context and grammar of the “numerical values” in your data.

The problem with the short answer is that cases like this change:

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

Yes, it is possible a typo (a space after a comma), but the data entered by a person often have such errors.

If you include more context, you are likely to reduce false positives. Such a pattern as

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

(which you can avoid the taste for your chosen programming language) will correspond only if the part of the “digit-comma” (with an optional sign and currency symbol) was limited to a space or the end of an element. If all your numeric values ​​are isolated inside XML elements, it will be easier for you.

+1
source

if it is not used anywhere except for the number on the line, you can use the following:

 string newStr = myInputString.Replace(",", "."); 
0
source

While you could theoretically do this with Regex, the template would be complex and difficult to test. ICR is on the right track, you need to do it based on culture.

Did you know that your numbers will always use a comma as a decimal point separator instead of a period? It looks like you can, considering that Navision is a Danish company.

If so, you need to go through the XML document in line and rewrite the numerical values. It looks like you can define this on the node name, so this will not be a problem.

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

here is 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 speaks of the TryParse () method, in which you convert a number from the Danish language (da-DK). After calling the function, you can use ToString () to write the number in your local format (which I assume is the USA or Canada) to get the period for your decimal separator. This also takes into account digits with a different separator per thousand digits (1 424.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".

0
source

All Articles