C# StringFormat 中的百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9751215/
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-09 08:42:17  来源:igfitidea点击:

Percentage in StringFormat

c#decimalstring-formatting

提问by Mironline

I have a technical problem with using percentage in StringFormatmethod.

我在StringFormat方法中使用百分比时遇到技术问题。

The result of String.Format("{0:P}", 0.8526)is 85.26%

结果String.Format("{0:P}", 0.8526)85.26%

but I think it should be 0.8526%

但我认为应该是 0.8526%

Is it correct ? If yes , How can I get 0.8526%?

这是正确的吗 ?如果是,我怎样才能得到0.8526%

采纳答案by Pandor Cai

Console.WriteLine(string.Format("{0}%", 0.8526));

Console.WriteLine(string.Format("{0}%", 0.8526));

回答by Kaf

Yes mathematically 0.8526 is equal to 85.26%. If you need to get 0.8526% then try this

是的,数学上 0.8526 等于 85.26%。如果你需要得到 0.8526% 然后试试这个

String.Format("{0:P}", 0.8526/100)

回答by Jordan

String.Formatwill multiply by 100 when you use % or :p String.FormatYou should divide by 100 first if you want to get what you are looking for.

String.Format当您使用 % 或 :p String.Format时将乘以 100 如果您想得到您想要的东西,您应该先除以 100。

回答by Michel Keijzers

A percentage is considered from 0-100% and the related floating pointer number from 0.0-1.0 as it is a ratio between two numbers.

百分比被认为是从 0-100% 和相关的浮点数从 0.0-1.0,因为它是两个数字之间的比率。