C# 从十进制值的末尾删除 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:34:50  来源:igfitidea点击:

Remove 0s from the end of a decimal value

c#decimal

提问by Fifty

I have a decimal value that has a variable number of digits after the ., for example:

我有一个十进制值,在 之后有可变位数.,例如:

0.0030
0.0310
0.0001
1.1200

How can I write a dynamic function that removes 0 in the end of the decimal?

如何编写一个删除小数点末尾的 0 的动态函数?

采纳答案by Jonas Elfstr?m

string.Format("{0:0.#####}", 0.0030)

or

或者

var money=1.3000m;
money.ToString("0.#####");

For future reference I recommend the .NET Format String Quick Referenceby John Sheehan.

为了将来参考,我推荐John Sheehan的.NET Format String Quick Reference

回答by Gabriel Magana

Hmm, this is a display formatting issue (the zeros are added when you convert the decimal to a string).

嗯,这是一个显示格式问题(将小数转换为字符串时会添加零)。

You need to see where in code you are seeing the trailing zeros. Is it after a call to .ToString()? Try playing around with the different formatting strings:

您需要查看在代码中看到尾随零的位置。是在调用 .ToString() 之后吗?尝试使用不同的格式字符串:

.ToString("#");
.ToString("0.00");
.ToString("#.##");

And so on. The best way to do this is just to experiment with the different possible values.

等等。最好的方法就是尝试不同的可能值。

回答by Martin Booth

decimal m = 0.030000m;
Console.Write(m.ToString("0.##########"));

Just make sure you have enough #s for the number of decimal places you want to display

只需确保您有足够的#s 来表示要显示的小数位数

回答by Andrew M

decimal value = 0.0030m;
value.ToString(“G29″);

Edit: The G formatter does work, the only problem is that it jumps to scientific notation if there are too many significant figures in the original decimal. Not so ideal.

编辑:G 格式化程序确实有效,唯一的问题是如果原始十进制中有太多有效数字,它会跳转到科学记数法。没那么理想。

See the "The General ("G") Format Specifier" documentation here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString

请参阅此处的“通用(“G”)格式说明符”文档:http: //msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString

I'm on lunch, so I did a little test:

我正在吃午饭,所以我做了一个小测试:

decimal d1 = 0.000100m;
decimal d2 = 0.001000000000000000000000m;
decimal d3 = 0.000000000000001000000000m;

Console.WriteLine(Environment.NewLine + "input decimal: 0.000100m");
Console.WriteLine("G         " + d1.ToString("G"));
Console.WriteLine("G29       " + d1.ToString("G29"));
Console.WriteLine("0.####### " + d1.ToString("0.#######"));

Console.WriteLine(Environment.NewLine + "input decimal: 0.001000000000000000000000m");
Console.WriteLine("G         " + d2.ToString("G"));
Console.WriteLine("G29       " + d2.ToString("G29"));
Console.WriteLine("0.####### " + d2.ToString("0.#######"));

Console.WriteLine(Environment.NewLine + "input decimal: 0.000000000000001000000000m");
Console.WriteLine("G         " + d3.ToString("G"));
Console.WriteLine("G29       " + d3.ToString("G29"));
Console.WriteLine("0.####### " + d3.ToString("0.#######"));

Output:

输出:

input decimal: 0.000100m
G         0.000100
G29       0.0001
0.####### 0.0001

input decimal: 0.001000000000000000000000m
G         0.001000000000000000000000
G29       0.001
0.####### 0.001

input decimal: 0.000000000000001000000000m
G         0.000000000000001000000000
G29       1E-15
0.####### 0

回答by Alain

I use the following. It ensures that any decimal (for which the max precision is 29 decimal places) will show all available digits of precision without trailing zeros, and without your code needing to have a long ugly string of hash marks.

我使用以下内容。它确保任何小数(最大精度为 29 位小数)将显示所有可用的精度数字,而没有尾随零,并且您的代码不需要有一长串难看的哈希标记。

if (value is Decimal)
   value = ((Decimal)value).ToString("0.".PadRight(29, '#'), culture);

回答by Thomas Materna

You can also modify the decimal itself so that any ToString() will give you what you want (more details in my answer here) :

您还可以修改小数点本身,以便任何的ToString()会给你你想要的东西(我的回答更多的细节在这里):

public static decimal Normalize(decimal value)
{
    return value/1.000000000000000000000000000000000m;
}

回答by VovaM

 public static string GentlyRemoveEndZeros(string input)
        {
          //  if (input == null) return null;
          //  if (input == "") return "";
            if (input.Contains(".")) return input.TrimEnd('0').TrimEnd('.');
            return input;
        }

回答by amedriveroperez

Truncate trailing Zeros is very easy, resolve with a duplex cast:

截断尾随零非常容易,使用双工转换解决:

    decimal mydecimal = decimal.Parse("1,45000000"); //(I)
    decimal truncate = (decimal)(double)mydecimal;   //(II)

(I) --> Parse decimal value from any string source.

(I) --> 从任何字符串源解析十进制值。

(II) --> First: Cast to double this remove the trailing zeros. Second: Other cast to decimal because dont exist implicit conversion from decimal to double and viceversa)

(II) --> 第一:强制转换成双倍删除尾随零。第二:其他转换为十进制,因为不存在从十进制到双精度的隐式转换,反之亦然)