如何格式化字符串的钱 [英] How to format string to money

查看:228
本文介绍了如何格式化字符串的钱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个字符串 000000000100 ,我想转换为1.00,反之亦然。



。前导零会删除,最后两位是小数



我给更多的例子:

  000000001000< = GT; 10.00 
000000001005< = GT; 10.05
000000331150< = GT; 3311.50



下面是我想要的代码,它给我带来不带小数点:



  amtf =的String.Format({0:0.00},amt.TrimStart(新的char [] {0})); 


解决方案

将字符串为十进制,然后除以100它并应用货币格式字符串:

 的String.Format({0:#00},Convert.ToDecimal(myMoneyString )/ 100); 



修改,以去除货币符号的要求和转换,而不是为十进制。


I have a string like 000000000100, which I would like to convert to 1.00 and vice versa.

Leading zero will be remove, last two digit is the decimal.

I give more example :

000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50

Below is the code I am trying, it is giving me result without decimal :

amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));

解决方案

Convert the string to a decimal then divide it by 100 and apply the currency format string:

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

Edited to remove currency symbol as requested and convert to decimal instead.

这篇关于如何格式化字符串的钱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆