首页 > 解决方案 > 如何将小数点放在整数中

问题描述

我正在从银行读取一个带有一串数字的文本文件。0000000010050,这个字符串的金额是$100.50,但是有没有办法在字符串中插入小数点呢?这是我的代码。

string amount = 0000000010050; 
string f_amount = "";
string ff_amount = "";
decimal d_amount = 0;

f_amount = amount.Trim();
d_amount = int.Parse(f_amount.TrimStart('0')); // this part removes the zeros and the output is 10050.
ff_amount = string.Format("{0:0,0.00}", d_amount); // this line outputs 10050.00

如何使输出看起来像这样100.50

标签: c#text-formattingasp.net-core-mvc-2.0

解决方案


首先将字符串转换为十进制,并以这种方式应用string.Format

string.Format("{0:#.00}", Convert.ToDecimal(bankString) / 100);

这将给出结果 100.50

https://dotnetfiddle.net/WrRVFo


推荐阅读