十进制转换为字符串使用非默认格式 [英] Converting Decimal to string with non-default format

查看:118
本文介绍了十进制转换为字符串使用非默认格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要十进制转换为字符串,在这个标尺:

  120.00  - 120
120.01 - 120.01
120.50 - 120.50


解决方案

您可以使用 Decimal.ToString的 覆盖指定格式

 小数金额=120.00米; 
字符串str = amount.ToString(0.00);



使用时,这也可以用来的String.Format

  Console.WriteLine({0:0.00},数量); 

在你的第一个规则的情况下,不能在一行中完成的。

 小数金额=120.00米; 
字符串str = amount.ToString(0.00)更换(,的String.Empty00)。


I need to convert decimal to string, in this rulers:

120.00 - "120"
120.01 - "120.01"
120.50 - "120.50"

解决方案

You can use the decimal.ToString override to specify a formatting.

decimal amount = 120.00m;
string str = amount.ToString("0.00");

This can also be used when using String.Format.

Console.WriteLine("{0:0.00}", amount); 

In the case of your first rule, it cannot be done on one line.

decimal amount = 120.00m;
string str = amount.ToString("0.00").Replace(".00", String.Empty);

这篇关于十进制转换为字符串使用非默认格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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