c# - 如何将小数点放在整数中
问题描述
我正在从银行读取一个带有一串数字的文本文件。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
?
解决方案
首先将字符串转换为十进制,并以这种方式应用string.Format
string.Format("{0:#.00}", Convert.ToDecimal(bankString) / 100);
这将给出结果 100.50
推荐阅读
- gitlab - 如何使多个目标的串行管道成为并行管道?
- excel - 用分隔符替换列中的第一个空格
- javascript - 无法使用量角器在文本框中输入文本
- kubernetes-ingress - Nginx 入口在 POST 后返回 502 并重定向
- amazon-web-services - 确认端点已在 SageMaker 笔记本中删除
- python - 我正在尝试使用 python 从 html 网站中提取一些数据
- c++ - 成功创建 c++ 类和对象
- python - 无法使用 Python 创建超过 1 个类实例
- validation - 自定义验证规则不适用于 Webix Colorpicker
- python - 在 pyspark 的 rdd 中选择两个元素