C# 十进制格式查询

C# Decimal Formatting Query(C# 十进制格式查询)
本文介绍了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# 十进制格式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)