C#小数点分隔符? [英] C# decimal separator?

查看:166
本文介绍了C#小数点分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回如下数字的方法:

I have a method which returns numbers like this:

public decimal GetNumber()
{
    return 250.00m;
}

现在,例如,将该值打印到控制台时,它带有逗号(250,00),而不是点(250.00)。我一直想在这里指出问题,我在做什么错了?

Now when this value is printed to the console for example, it has a comma (250,00) instead of a point (250.00). I always want a point here, what am I doing wrong?

推荐答案

十进制本身没有格式-它既没有逗号也没有点。

decimal itself doesn't have formatting - it has neither a comma nor a dot.

只有在将其转换为字符串时,您才能得到它。您可以通过指定不变的区域性来确保得到一个点。

It's when you convert it to a string that you'll get that. You can make sure you get a dot by specifying the invariant culture:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
        decimal d = 5.50m;
        string withComma = d.ToString();
        string withDot = d.ToString(CultureInfo.InvariantCulture);
        Console.WriteLine(withComma);
        Console.WriteLine(withDot);
    }
}

这篇关于C#小数点分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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