简体   繁体   English

使用逗号格式化十进制,保留尾随零

[英]Format decimal with commas, preserve trailing zeros

I'd like to convert a decimal to a string, with commas as thousands seperators, and preserve the same precision the decimal was created with. 我想将小数转换为字符串,逗号为数千个分隔符,并保留创建小数的相同精度。 (Will have 2-5 significant digits) (将有2-5位有关数字)

        decimal d = 1234.4500M;

        //I'd like "1,234.4500"

        var notRight = d.ToString("###,###.#######");     //1,234.45
        var alsoNotRight = d.ToString("###,###.00000");;  //1,234.45000
        var notRightEither = d.ToString("N");    //1,234.45
        var notRightEither2 = d.ToString("G");   //1234.45000

Is there no built in way to do this without parsing the string manually? 如果不手动解析字符串,是否没有内置方法可以做到这一点? If there is no single format string, what's the easiest way to do this? 如果没有单一格式字符串,最简单的方法是什么?

According to the documentation , a decimal number preserves the trailing zeros. 根据文档 ,十进制数保留尾随零。 You can display them if you use the "G" specifier or no specifier at all. 如果使用“G”说明符或根本没有说明符,则可以显示它们。 They are lost when you use one of the specifiers that includes a thousand separator. 当您使用包含千位分隔符的说明符之一时,它们会丢失。

If you want to specify the number of trailing zeros when converting to string, you can do it by adding a precision specifier (0 to 99 digits) after the format character, like this: 如果要在转换为字符串时指定尾随零的数量,可以通过在格式字符后添加精度说明符 (0到99位)来完成,如下所示:

decimal d=1234.45M;
var numberAsString=d.ToString("N4");

The result will be 结果将是

 1,234.4500

UPDATE: You can get the number of decimal digits using the Decimal.GetBits method which returns the binary representation of the number. 更新:您可以使用Decimal.GetBits方法获取小数位数,该方法返回数字的二进制表示形式。 The number of decimals is stored in bits 16-23 (third byte) of the fourth element. 小数位数存储在第四个元素的位16-23(第三个字节)中。

The fourth element of the returned array contains the scale factor and sign. It consists of the following parts:

... ...

Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.

Getting a string representation using all digits can be done like this: 使用所有数字获取字符串表示可以这样做:

decimal d=1234.45000M;
var nums=Decimal.GetBits(d);
var decimals=BitConverter.GetBytes(nums[3])[2];
var formatStr="N"+decimals;
d.ToString(formatStr);

which will produce 这将产生

1,234.45000

Since you plan on having a variable number of decimal places (2-5) , I don't think you can pull it off via a string format. 由于您计划使用可变数量的小数位(2-5) ,我认为您不能通过字符串格式将其拉出。

This solution is not necessarilly pretty, but it gets the job done. 这个解决方案并不是必需的,但它可以完成工作。 Do note that it will allocate several strings in the process (I believe 5), so it may not perform well in large-scale use. 请注意,它将在过程中分配几个字符串(我相信5),因此在大规模使用中可能效果不佳。 You will retain the number of decimal places, and get comma separated groups in the portion before the decimal. 您将保留小数位数,并在小数点前的部分中获取逗号分隔的组。

public static string FormatDecimal(decimal d)
{
    return d.ToString("N0") + // Format portion before decimal
           "." + 
           d.ToString().Split('.')[1];  // Retain number of decimal places
}

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

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