Looking to convert this to a string value of 2.68. I have a way to parse it out but was wondering if there was some built in functionality in the framework to do this.
views:
110answers:
3
+1
A:
Why not turn it into a number, divide by 100,000 like Dominic suggested and then format the number back into a string with the appropriate number of decimal places?
Twotymz
2009-08-13 16:00:00
+3
A:
Untested, uing the builtin function Int32.Parse:
string convert_so_1272865_v1(string s){
return ((Int32.Parse(s)/1000)/100.0).ToString();
}
And a version without any parsing:
string convert_so_1272865_v2(string s){
return s.SubString(0,1) + "." + s.SubString(1,2);
}
Martin v. Löwis
2009-08-13 16:00:51
Should work if truncating is desired. Possibly, using `Math.Floor` or `Math.Round` would be the intended behavior.
Thorarin
2009-08-13 16:50:45
+1
A:
Assuming that the number is just a fixed point fixed width number with 1 digit left and 5 digits right of the decimal point, try (Decimal.Parse("268179") / 100000D).Round(2)
Yuliy
2009-08-13 16:02:29
Inserting a "." into a string is less efficient than doing arithmetic operations.
Richard Hein
2009-08-13 17:31:13