如何在double.ToString()中更改小数点的符号? [英] How to change symbol for decimal point in double.ToString()?

查看:181
本文介绍了如何在double.ToString()中更改小数点的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中将小数点改为另一个字符。我有一个 double 变量

  double value; 

当我使用命令:

  Console.WriteLine(value.ToString()); //输出是1,25 

我知道我可以这样做:

  Console.WriteLine(value.ToString(
CultureInfo.CreateSpecificCulture(en-GB))); //输出是1.25

但我不喜欢它,因为它很长,我需要在我的程序中经常出现。



是否有更短的版本设置小数点真的是点,而不是逗号,在我的文化中是平常的?

解决方案

一些快捷方式是创建一个NumberFormatInfo类,将其NumberDecimalSeparator属性设置为。并且只要你需要,就使用Class作为ToString()方法的参数。

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

value.ToString(nfi);


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

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?

解决方案

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 nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ".";

value.ToString(nfi);

这篇关于如何在double.ToString()中更改小数点的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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