从字符串中用美分获取美元价值的最佳方法是什么?

What is the best method to obtain dollar value with cents from a string?

我希望获得最优雅的解决方案(最少的输入代码)来满足将便士字符串转换为美分值的要求。它必须表示美分 (.00),因此除以 100 并不直接。例如“00000600000”必须表示为“6000.00”。我制作了以下方法:

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, ".") 
int cents = int.Parse(line);
decimal dollars = cents / 100m;
string value = dollars.ToString("$#.00");

或者,如果您对 优雅 的定义意味着 "fewer lines":

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

尝试以下方法

Dim str as string = "0006500000"
Dim d as decimal = cdec(str) / 100
Msgbox(d.tostring("#.##"))