简体   繁体   English

C# 用千位分隔符格式化数字,但保留小数位数

[英]C# formatting a number with thousand separators but keeping the decimal places count as is

I want to add a thousands separator to a double number but want to keep the decimal places as is ie dont want any rounding.我想将千位分隔符添加到double数,但希望保留小数位,即不希望四舍五入。

#,# is solving my problem of adding the thousand separator but how do I preserve the decimal places? #,#正在解决我添加千位分隔符的问题,但如何保留小数位? #,# strips off the part after . #,#去掉 . 之后的部分. . .

I cannot use any culture or something like that & the developer whose function I am calling has only given me a way of changing the format by passing as parameter strFormat .我不能使用任何文化或类似的东西 & 我打电话给 function 的开发人员只给了我一种通过传递参数strFormat来更改格式的方法。

I did check other posts & even the docs but somehow not able to figure this out.我确实检查了其他帖子甚至文档,但不知何故无法弄清楚。

string strFormat = "#,#";
string str = double.parse("912123456.1123465789").ToString(strFormat);
 //Expected here 912,123,456.1123465789 
//Actual Output 912,123,456

//912123456.123 should give 912,123,456.123
//912123456.1 should give 912,123,456.1
//912123456.1123465789 should give 912,123,456.1123465789
//912123456 should give 912,123,456

If you know the max number of decimal places, eg 10, then use:如果您知道最大小数位数,例如 10,则使用:

string strFormat = "#,#0.##########";

Update:更新:

This max number is known.这个最大数量是已知的。 According to Microsoft documentation a Double value has up to 15 decimal digits of precision (including both before and after the decimal point).根据Microsoft 文档,Double 值最多具有 15 个小数位的精度(包括小数点前后)。 More than 15 digits will be rounded.超过 15 位将四舍五入。

So if you must invoke that method of 'double.parse' and can only send the format, this is the best you can do:因此,如果您必须调用“double.parse”的方法并且只能发送格式,那么这是您可以做的最好的事情:

string strFormat = "#,#0.###############";

Well, after looking at the documentation on Microsoft , it would appear that there is no particular way to allow a floating point position in a number - all characters in a format string are character placeholders.好吧,在查看了Microsoft上的文档之后,似乎没有特定的方法允许数字中出现浮点数 position - 格式字符串中的所有字符都是字符占位符。

I would recommend that you either use a very nasty predetermined number of # s to set the width of the decimal position, or the slightly less (or possibly more, depending on your outlook) nasty option of reading all numbers into an array, determining the longest decimal position, then building a format of # s using the result.我建议您要么使用非常讨厌的预定数量的#来设置小数点 position 的宽度,要么使用稍微少一点(或者可能更多,取决于您的前景)的讨厌选项,将所有数字读入一个数组,确定最长的小数 position,然后使用结果构建#的格式。

At the end of the day, this is a single format string that you can put into place, test and ensure it works, then come back later and fix if you find a better alternative.归根结底,这是一个单一的格式字符串,您可以将其放置到位、测试并确保其正常工作,然后稍后回来并在找到更好的替代方案时进行修复。

Also, this is one of those things where you could put the string into a configuration setting and change as and when you need to - far more flexible.此外,这是您可以将字符串放入配置设置并根据需要进行更改的事情之一 - 更加灵活。

To be honest, this is a very slight thing to be worried about in the grand scheme of performance and writing a program.老实说,在性能和编写程序的宏伟计划中,这是一件非常小的事情。

Technically, Udi Y gets my vote!从技术上讲,Udi Y 得到了我的投票!

You can calculate the formatting dynamically for each number:您可以为每个数字动态计算格式:

public static void Main()
{
    var number = 1234.12312323123;

    var format = GetNumberFormat(number);
    Console.WriteLine(number.ToString(format));
}

public static string GetNumberFormat(double number)
{
    var numberAsString = number.ToString();

    var decimalPartSize = numberAsString.Substring(numberAsString.LastIndexOf('.') + 1).Length;

    return $"N{decimalPartSize}";
}

So所以

number = 1234.12312323123

will give you 1,234.12312323123 .会给你1,234.12312323123 Works for negative numbers as well.也适用于负数。 Also, as we work with strings, there won't be any rounding errors or precision artifacts.此外,当我们使用字符串时,不会有任何舍入错误或精度伪影。

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

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