如何在double.ToString()中更改小数点的符号?

时间:2022-05-07 17:07:23

I would like to change decimal point to another character in C#. I have a double variable value

我想将小数点更改为C#中的另一个字符。我有一个双变量值

double value;

and when I use the command:

当我使用命令时:

Console.WriteLine(value.ToString()); // output is 1,25

I know I can do this:

我知道我可以这样做:

Console.WriteLine(value.ToString(
    CultureInfo.CreateSpecificCulture("en-GB"))); // output is 1.25

but I don't like it very much because it's very long and I need it quite often in my program.

但是我不太喜欢它,因为它很长,而且在我的程序中经常需要它。

Is there a shorter version for setting "decimal point" really as point and not comma as is in my culture is usual?

是否有一个较短的版本设置“小数点”真的作为点而不是逗号,因为在我的文化中是常见的?

5 个解决方案

#1


86  

Some shortcut is to create a NumberFormatInfo class, set its NumberDecimalSeparator property to "." and use the class as parameter to ToString() method whenever u need it.

一些捷径是创建一个NumberFormatInfo类,将其NumberDecimalSeparator属性设置为“。”。并在需要时使用类作为ToString()方法的参数。

using System.Globalization;

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

value.ToString(nfi);

#2


33  

Create an extension method?

创建扩展方法?

Console.WriteLine(value.ToGBString());

// ...

public static class DoubleExtensions
{
    public static string ToGBString(this double value)
    {
        return value.ToString(CultureInfo.GetCultureInfo("en-GB"));
    }
}

#3


16  

Perhaps I'm misunderstanding the intent of your question, so correct me if I'm wrong, but can't you apply the culture settings globally once, and then not worry about customizing every write statement?

也许我误解了你的问题的意图,所以如果我错了就纠正我,但你不能在全球范围内应用文化设置一次,然后不用担心自定义每个写声明吗?

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

#4


15  

You can change the decimal separator by changing the culture used to display the number. Beware however that this will change everything else about the number (eg. grouping separator, grouping sizes, number of decimal places). From your question, it looks like you are defaulting to a culture that uses a comma as a decimal separator.

您可以通过更改用于显示数字的区域性来更改小数点分隔符。但请注意,这将改变有关数字的所有其他内容(例如,分组分隔符,分组大小,小数位数)。从您的问题来看,您似乎默认使用逗号作为小数分隔符的文化。

To change just the decimal separator without changing the culture, you can modify the NumberDecimalSeparator property of the current culture's NumberFormatInfo.

若要仅更改小数点分隔符而不更改区域性,可以修改当前区域性NumberFormatInfo的NumberDecimalSeparator属性。

Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

This will modify the current culture of the thread. All output will now be altered, meaning that you can just use value.ToString() to output the format you want, without worrying about changing the culture each time you output a number.

这将修改线程的当前文化。现在所有输出都会被更改,这意味着您只需使用value.ToString()输出所需的格式,而无需担心每次输出数字时都要更改文化。

(Note that a neutral culture cannot have its decimal separator changed.)

(请注意,中性文化不能更改其小数分隔符。)

#5


1  

If you have an Asp.Net web application, you can also set it in the web.config so that it is the same throughout the whole web application

如果您有一个Asp.Net Web应用程序,您也可以在web.config中设置它,以便它在整个Web应用程序中都是相同的

<system.web>
    ...
    <globalization 
        requestEncoding="utf-8" 
        responseEncoding="utf-8" 
        culture="en-GB" 
        uiCulture="en-GB" 
        enableClientBasedCulture="false"/>
    ...
</system.web>

#1


86  

Some shortcut is to create a NumberFormatInfo class, set its NumberDecimalSeparator property to "." and use the class as parameter to ToString() method whenever u need it.

一些捷径是创建一个NumberFormatInfo类,将其NumberDecimalSeparator属性设置为“。”。并在需要时使用类作为ToString()方法的参数。

using System.Globalization;

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

value.ToString(nfi);

#2


33  

Create an extension method?

创建扩展方法?

Console.WriteLine(value.ToGBString());

// ...

public static class DoubleExtensions
{
    public static string ToGBString(this double value)
    {
        return value.ToString(CultureInfo.GetCultureInfo("en-GB"));
    }
}

#3


16  

Perhaps I'm misunderstanding the intent of your question, so correct me if I'm wrong, but can't you apply the culture settings globally once, and then not worry about customizing every write statement?

也许我误解了你的问题的意图,所以如果我错了就纠正我,但你不能在全球范围内应用文化设置一次,然后不用担心自定义每个写声明吗?

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

#4


15  

You can change the decimal separator by changing the culture used to display the number. Beware however that this will change everything else about the number (eg. grouping separator, grouping sizes, number of decimal places). From your question, it looks like you are defaulting to a culture that uses a comma as a decimal separator.

您可以通过更改用于显示数字的区域性来更改小数点分隔符。但请注意,这将改变有关数字的所有其他内容(例如,分组分隔符,分组大小,小数位数)。从您的问题来看,您似乎默认使用逗号作为小数分隔符的文化。

To change just the decimal separator without changing the culture, you can modify the NumberDecimalSeparator property of the current culture's NumberFormatInfo.

若要仅更改小数点分隔符而不更改区域性,可以修改当前区域性NumberFormatInfo的NumberDecimalSeparator属性。

Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

This will modify the current culture of the thread. All output will now be altered, meaning that you can just use value.ToString() to output the format you want, without worrying about changing the culture each time you output a number.

这将修改线程的当前文化。现在所有输出都会被更改,这意味着您只需使用value.ToString()输出所需的格式,而无需担心每次输出数字时都要更改文化。

(Note that a neutral culture cannot have its decimal separator changed.)

(请注意,中性文化不能更改其小数分隔符。)

#5


1  

If you have an Asp.Net web application, you can also set it in the web.config so that it is the same throughout the whole web application

如果您有一个Asp.Net Web应用程序,您也可以在web.config中设置它,以便它在整个Web应用程序中都是相同的

<system.web>
    ...
    <globalization 
        requestEncoding="utf-8" 
        responseEncoding="utf-8" 
        culture="en-GB" 
        uiCulture="en-GB" 
        enableClientBasedCulture="false"/>
    ...
</system.web>