C# Decimal Formatting Query(C# 十进制格式查询)
问题描述
我正在尝试格式化小数,以便它显示如下:
I am trying to format a decimal so that it will get displayed as so:
14.5 应该显示为14.50"14.50 应显示为14.50"14.05 应该显示为14.05"14.00 应该显示为14"
14.5 should get displayed as "14.50" 14.50 should get displayed as "14.50" 14.05 should get displayed as "14.05" 14.00 should get displayed as "14"
是否可以使用单一字符串格式,即不使用条件格式?
Is the possible with a single String Format, i.e. not using conditional formatting?
我试过0.##"(不满足第一个例子)和0.00"(不满足最后一个例子).
I've tried "0.##" (doesn't satisfy the first example) and "0.00" (doesn't satisfy the last example).
谢谢.
推荐答案
是的.您可以使用G"格式说明符.有关示例,请参阅 String.Format 的文档.
Yes. You can use the "G" format specifier. For samples, see String.Format's documentation.
例如:
decimal value = 14.05m;
string result = String.Format("{0:G}", value); // "14.05"
value = 14m;
result = String.Format("{0:G}", value); // "14"
这篇关于C# 十进制格式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 十进制格式查询


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01