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
AndreasPosted Jun 7, 2010, 8:35 AM
decimal result = decimal.Truncate( ValueToTruncate * 100 ) / 100;
theLizardPosted Dec 17, 2009, 6:47 PM
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!!!
Kirtan PatelPosted Dec 17, 2009, 12:55 PM
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 :)
Tomasz KornackiPosted Dec 17, 2009, 12:37 PM
You could multiply by 100, cast to int and divide by 100.0.
AlanPosted Aug 1, 2008, 9:37 AM
Check out the Math.Round() method:
http://msdn.microsoft.com/en-us/library/system.math.round.aspx