Hi Everyone,
I was wondering if someone could help me. I am recieving prices from a XML steam and I am wondering how to strip off some decimal places.
The price I get back looks like 12.5210125 and I only want to keep 2 decimal places. How can I strip off the extra decimal places so I get 12.52 Could I use a Trim of a Substring method?
Thanks in advance.
Jay

AlanRank #669 · 3.6K reputationPosted Aug 1, 2008, 5:37 AM EDT
Check out the Math.Round() method:
http://msdn.microsoft.com/en-us/library/system.math.round.aspx
Tomasz Kornacki22 reputationPosted Dec 17, 2009, 7:37 AM EST
You could multiply by 100, cast to int and divide by 100.0.
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 17, 2009, 7:55 AM EST
decimal DataFromXml = Convert.ToDecimal("12.5210125");
//Get Rounded Value 12.52
decimal x = Math.Round(DataFromXml,2);
friend, if it helps you then check "Do you like this answer" check box :)
theLizardRank #1,079 · 1.8K reputationPosted Dec 17, 2009, 1:47 PM EST
DataFromXMl = DataFromXml.SubString(0, DataFromXml.IndexOF(".") + 2);
This returns a string beginning with the first char to a length 2 greater than the length of string at index of '.'
Of course if you do want it in a decimal form, you could do something like this
decimal b = decimal.parse(DataFromXMl = DataFromXml.SubString(0, DataFromXml.IndexOF(".") + 2));
No point converting from one to the another just to convert back to a string!!!
Andreas5 reputationPosted Jun 7, 2010, 4:35 AM EDT
decimal result = decimal.Truncate( ValueToTruncate * 100 ) / 100;