格式化十进制 C#

编程入门 行业动态 更新时间:2024-10-24 10:26:02
本文介绍了格式化十进制 C# - 保留最后一个零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在寻找这个,但似乎无法找到答案.我有以下小数和我想要的 String.Format

I have been searching for this but cannot seem to find the answer. I have the following decimals with the corresponding output that I want from String.Format

100.00 -> 100100.50 -> 100.50100.51 -> 100.51

100.00 -> 100 100.50 -> 100.50 100.51 -> 100.51

我的问题是,我似乎找不到一种格式,既可以在 100.50 的末尾保留 0,又可以从 100 中删除 2 个零.

My problem is that I cannot seem to find a format which will keep the 0 on the end of 100.50 as well as remove the 2 zeros from 100.

非常感谢任何帮助.

编辑为了更清楚一些.我有十进制类型的变量,它们只能保留 2 个小数位.基本上我想显示2个小数位,如果它们存在或不存在,我不想在100.50变成100.5的情况下显示一个小数位

EDIT For some more clarity. I have variables of type decimal, they are only ever going to be 2 decimal places. Basically I want to display 2 decimal places if they exist or none, I don't want to display one decimal place in the case of 100.50 becoming 100.5

推荐答案

你可以使用这个:

string s = number.ToString("0.00"); if (s.EndsWith("00")) { s = number.ToString("0"); }

更多推荐

格式化十进制 C#

本文发布于:2023-07-27 12:01:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!