Ken Mattingly - 10 months ago 54

ASP.NET (C#) Question

I'm hoping to obtain the most elegant solution for the requirement of converting a penny string into a dollar cent value. It must represent the cents (.00), so dividing by 100 is not straight forward. For example "00000600000" has to be represented as "6000.00". I've produced the following methods:

`value = Math.Abs(Int(Mid(Mid(line, 188, 11), 1, Len(Mid(line, 188, 11)) - 2))) & "." & Mid(Mid(line, 188, 11), Len(Mid(line, 188, 11)) - 1, 2)`

value = (Math.Abs(Int(Mid(line, 188, 11))).ToString).Insert(Math.Abs(Int(Mid(line, 188, 11))).ToString.Length - 2, ".")

value = Math.Abs(Int(Mid(line, 188, 11))) : value = value.Insert(value.Length - 2, ".")

Answer Source

```
int cents = int.Parse(line);
decimal dollars = cents / 100m;
string value = dollars.ToString("$#.00");
```

Or, if your definition of *elegant* implies "fewer lines":

```
value = (int.Parse(line) / 100m).ToString("$#.00");
```