简体   繁体   English

格式十进制到没有科学记数法的小数位数

[英]Format decimal to number of decimal places without scientific notation

I am currently formatting a double using the code: 我目前正在使用代码格式化double:

myDouble.ToString("g4");

To get the first 4 decimal places. 获得前4位小数。 However I find this often switches over to scientific notation if the number is very large or very small. 但是我发现,如果数字非常大或非常小,我会经常切换到科学记数法。 Is there an easy format string in C# to just have the first four decimal places, or zero if it is too small to be represented in that number of places? C#中是否有一个简单的格式字符串,只有前四个小数位,如果它太小而无法在该位数中表示,则为零?

For example, I would like: 例如,我想:

1000 => 1000
0.1234567 => 0.1235
123456 => 123456 (Note: Not scientific notation)
0.000001234 => 0 (Note: Not scientific notation)

You can try like this: 你可以尝试这样:

0.1234567.ToString("0.####")

Also check Custom Numeric Format Strings 还要检查自定义数字格式字符串

#

Replaces the "#" symbol with the corresponding digit if one is present; 将“#”符号替换为相应的数字(如果存在); otherwise, no digit appears in the result string. 否则,结果字符串中不会出现数字。

Also as Jon as correctly pointed that it will round your number. 同样正如乔恩所指出的那样,它将围绕你的数字。 See the note section 请参阅注释部分

Rounding and Fixed-Point Format Strings 舍入和定点格式字符串

For fixed-point format strings (that is, format strings that do not contain scientific notation format characters), numbers are rounded to as many decimal places as there are digit placeholders to the right of the decimal point. 对于定点格式字符串(即不包含科学记数法格式字符的格式字符串),数字四舍五入到小数点右边的数字占位符的小数位数。

Use the String.Format() method. 使用String.Format()方法。

String.Format("{0:0.####}", 123.4567123); //output: 123.4567

Note: Num of #'s indicate the maximum number of digits after decimal that are required. 注意:#的数字表示所需的小数点后的最大位数。

I agree with kjbartel comment. 我同意kjbartel的评论。 I wanted exactly what the original question asked. 我想要原来的问题。 But his question is slightly ambiguous. 但他的问题有点含糊不清。

The problem with ### format is it fills the slot if a digit can be represented or not. ###格式的问题是,如果可以表示数字,则填充插槽。 So it does what the original question asks for some numbers but not others. 所以它确实原始问题要求一些数字而不是其他数字。

My basic need is, and it's a pretty common one, if the number is big I don't need to show decimal places. 我的基本需求是,它是一个非常常见的,如果数字很大我不需要显示小数位。 If the number is small I do want to show decimal places. 如果数字很小,我想显示小数位。 Basically X number of significant digits. 基本上是X个有效数字。

The "Gn" Format will do significant digits, but it switches to scientific notation if you go over the number of digits. “Gn”格式将显示有效数字,但如果超过位数,则会切换为科学记数法。 I don't want E notation, ever (same requirement as the question). 我不想要E符号(与问题相同的要求)。

So I used fixed format ("Fn") but I calculate the width on the fly based on how "big" the number is. 所以我使用了固定格式(“Fn”),但我根据数字的“大”来计算宽度。

var myFloatNumber = 123.4567;
var digits = (int) Math.Log10(myFloatNumber);
var maxDecimalplaces = 3;
var format = "F" + Math.Max(0,(maxDecimalplaces - digits));

I swear there was a way to do this in C++ (Visual Studio Flavor) in teh format statement or in C# and perhaps there is, but I can't find it. 我发誓有一种方法可以在C ++(Visual Studio Flavor)中使用格式语句或在C#中执行此操作,也许有,但我找不到它。

So I came up with this. 所以我想出了这个。 I could have converted to a string and measured length before decimal point as well. 我可以转换为字符串并测量小数点前的长度。 But converting it to a string twice felt wrong. 但把它转换成字符串两次感觉不对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM