.NET字符串格式化程序中的可变小数位? [英] variable decimal places in .Net string formatters?

查看:67
本文介绍了.NET字符串格式化程序中的可变小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

固定小数位很容易

String.Format("{0:F1}", 654.321);

给予

654.3

如何像输入参数一样输入小数位数在C中?
So

How do I feed the number of decimal places in as a parameter like you can in C? So

String.Format("{0:F?}", 654.321, 2);

给予

654.32

我找不到应该替代什么?

I can't find what should replace the ?

推荐答案

使用 NumberFormatInfo

Console.WriteLine(string.Format(new NumberFormatInfo() { NumberDecimalDigits = 2 }, "{0:F}", new decimal(1234.567)));
Console.WriteLine(string.Format(new NumberFormatInfo() { NumberDecimalDigits = 7 }, "{0:F}", new decimal(1234.5)));

这篇关于.NET字符串格式化程序中的可变小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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