Double.ToString 用于格式化

发布于 2024-10-21 16:39:04 字数 301 浏览 1 评论 0原文

我需要使用特定格式格式化数字。我不想用计算机的区域设置来决定格式,所以我正在尝试:

string s = "219171"
string result = Convert.ToDouble(s).ToString("0,0.0") //219,171.0
string result = Convert.ToDouble(s).ToString("0.0,0") //219171.00

我想将其显示为

219.171,00

谢谢

I need to format a number with a specific format. I don't want to decide on the format with the regional settings of the computer so I'm trying:

string s = "219171"
string result = Convert.ToDouble(s).ToString("0,0.0") //219,171.0
string result = Convert.ToDouble(s).ToString("0.0,0") //219171.00

I want to display it as

219.171,00

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我ぃ本無心為│何有愛 2024-10-28 16:39:04

创建一个自定义 NumberFormatInfo 实例并在调用 ToString() 时将其传入。

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ".";
nfi.NumberDecimalSeparator = ",";

double s = 219171;
string result = s.ToString("N2", nfi);

NumberFormatInfo 属于 System.Globalization

Create a custom NumberFormatInfo instance and pass that in when calling ToString().

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ".";
nfi.NumberDecimalSeparator = ",";

double s = 219171;
string result = s.ToString("N2", nfi);

NumberFormatInfo belongs to System.Globalization

够运 2024-10-28 16:39:04

您可以指定要在 Double.ToString(IFormatProviderprovider) 重载中格式化数字的区域设置。如果您不提供它,将使用 Thread.CurrentThread.CurrentUILocale 。如果您希望它独立于计算机设置,可以使用Double.ToString(CultureInfo.InvariantCulture)

如果您想完全自定义格式(例如,小数分隔符之前/之后恰好是 x 位),请在 格式字符串概述

You can specify the locale for which you want the number formatted in the Double.ToString(IFormatProvider provider) overload. It you don't supply it, Thread.CurrentThread.CurrentUILocale will be used. If you want it to be independant of the computer settings, you can use Double.ToString(CultureInfo.InvariantCulture).

If you want to completely customize the formatting (e. g. exactly x digit before/after the decimal seperator), look it up in a formatstring overview.

云巢 2024-10-28 16:39:04

下面的方法应该可以解决这个问题:

string result = (Convert.ToDouble(s) / 1000).ToString("##0.000\\,00");

除以 1000 会改变小数点的位置。每个 # 或 0 都是一个数字。

或者,您可以使用:

string result = Convert.ToDouble(s).ToString("[email protected]").Replace('.', ',').Replace('@', '.');

The following should do the trick:

string result = (Convert.ToDouble(s) / 1000).ToString("##0.000\\,00");

Dividing 1000 changes the location of the decimal point. Each # or 0 is a digit.

Alternatively, you could use:

string result = Convert.ToDouble(s).ToString("[email protected]").Replace('.', ',').Replace('@', '.');
一个人的夜不怕黑 2024-10-28 16:39:04

我可能会这样做:

// save parsing, ignoring current settings
double d = double.Parse(s, CultureInfo.InvariantCulture);
// whatever culture you actually want, assuming German
var culture = CultureInfo.GetCultureInfo("de");
string formatted = d.ToString(culture);

I would probably do it like this:

// save parsing, ignoring current settings
double d = double.Parse(s, CultureInfo.InvariantCulture);
// whatever culture you actually want, assuming German
var culture = CultureInfo.GetCultureInfo("de");
string formatted = d.ToString(culture);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文