Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change values in string from 0,00 to 0.00

Tags:

c#

.net

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

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>";
like image 513
user61186 Avatar asked Dec 13 '22 05:12

user61186


2 Answers

Try this :

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

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

like image 111
Canavar Avatar answered Dec 30 '22 10:12

Canavar


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);
like image 26
ICR Avatar answered Dec 30 '22 10:12

ICR