views:

110

answers:

3

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.

+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
+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
Should work if truncating is desired. Possibly, using `Math.Floor` or `Math.Round` would be the intended behavior.
Thorarin
+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
Why not add the dot and then parse? ("268179").Insert(1,".")
Miky Dinescu
Inserting a "." into a string is less efficient than doing arithmetic operations.
Richard Hein