C#怎样的String.Format无限小数位小数? [英] c# how to String.Format decimal with unlimited decimal places?

查看:1144
本文介绍了C#怎样的String.Format无限小数位小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个十进制数转换为格式化字符串一千组和无限(变量)十进制数:

 

1234 - >1,234
1234.567 - >1,234.567
1234.1234567890123456789 - >1,234.1234567890123456789

我试过的String.Format({0:#,##},十进制)。,但它修剪任何数量最多1位小数


< DIV CLASS =h2_lin>解决方案

您可以使用#多次(见自定义数字格式字符串):

 的String.Format({0:#,#####。 #########################},decimalValue)

或者,如果你只是直接格式化数字,你也可以只使用的 Decimal.ToString的与格式字符串。



但是,没有办法,包括无限的十进制数。如果没有库支持任意精度浮点数(例如,使用像 BigFloat从 至尊Numerics的),你会碰到的问题精度最终。即使是小数类型有其精度极限(28-29显著位)。除此之外,你会遇到其他问题。


I need to convert a decimal number to formatted string with thousand groups and unlimited (variable) decimal numbers:


    1234 -> "1,234"
    1234.567 -> "1,234.567"
    1234.1234567890123456789 -> "1,234.1234567890123456789"

I tried String.Format("{0:#,#.#}", decimal), but it trims any number to max 1 decimal place.

解决方案

You can use # multiple times (see Custom Numeric Format Strings):

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

Or, if you're just formatting a number directly, you can also just use decimal.ToString with the format string.

However, there is no way to include "unlimited decimal numbers". Without a library supporting arbitrary precision floating point numbers (for example, using something like BigFloat from Extreme Numerics), you'll run into precision issues eventually. Even the decimal type has a limit to its precision (28-29 significant digits). Beyond that, you'll run into other issues.

这篇关于C#怎样的String.Format无限小数位小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆