Java DecimalFormat 的 C# 等效项是什么?

发布于 2024-08-08 03:59:31 字数 596 浏览 4 评论 0原文

我如何将以下代码转换为 C#

DecimalFormat form 
String pattern = "";
for (int i = 0; i < nPlaces - nDec - 2; i++) {
        pattern += "#";
}
pattern += "0.";
for (int i = nPlaces - nDec; i < nPlaces; i++) {
        pattern += "0";
}
form = (DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols = form.getDecimalFormatSymbols();
symbols.setDecimalSeparator('.');
form.setDecimalFormatSymbols(symbols);
form.setMaximumIntegerDigits(nPlaces - nDec - 1);
form.applyPattern(pattern);

编辑 特殊的问题是我不希望小数点分隔符随区域设置而更改(例如,某些区域设置会使用“,”)。

How would I convert the following code to C#

DecimalFormat form 
String pattern = "";
for (int i = 0; i < nPlaces - nDec - 2; i++) {
        pattern += "#";
}
pattern += "0.";
for (int i = nPlaces - nDec; i < nPlaces; i++) {
        pattern += "0";
}
form = (DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols = form.getDecimalFormatSymbols();
symbols.setDecimalSeparator('.');
form.setDecimalFormatSymbols(symbols);
form.setMaximumIntegerDigits(nPlaces - nDec - 1);
form.applyPattern(pattern);

EDIT The particular problem is that I do not wish the decimal separator to change with Locale (e.g. some Locales would use ',').

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

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

发布评论

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

评论(2

ㄟ。诗瑗 2024-08-15 03:59:31

对于小数点分隔符,您可以在 NumberFormatInfo 实例中设置它,并将其与 ToString 一起使用:

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

    //** test **
    NumberFormatInfo nfi = new NumberFormatInfo();
    decimal d = 125501.0235648m;
    nfi.NumberDecimalSeparator = "?";
    s = d.ToString(nfi); //--> 125501?0235648

要获得 java 版本的结果,请使用 ToString() 函数版本与 自定义数字格式字符串(即:您所说的模式):

s = d.ToString("# ### ##0.0000", nfi);// 1245124587.23     --> 1245 124 587?2300
                                      //      24587.235215 -->       24 587?2352

System.Globalization.NumberFormatInfo

For decimal separator you can set it in a NumberFormatInfo instance and use it with ToString:

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

    //** test **
    NumberFormatInfo nfi = new NumberFormatInfo();
    decimal d = 125501.0235648m;
    nfi.NumberDecimalSeparator = "?";
    s = d.ToString(nfi); //--> 125501?0235648

to have the result of your java version, use the ToString() function version with Custom Numeric Format Strings (i.e.: what you called pattern):

s = d.ToString("# ### ##0.0000", nfi);// 1245124587.23     --> 1245 124 587?2300
                                      //      24587.235215 -->       24 587?2352

System.Globalization.NumberFormatInfo

德意的啸 2024-08-15 03:59:31

在 C# 中,十进制数存储在decimal 类型,具有内部表示形式,允许您执行十进制数学运算而不会出现舍入错误。

获得号码后,您可以格式化它使用 Decimal.ToString() 进行输出。这种格式是特定于语言环境的;它尊重您的当前的文化设置

In C#, decimal numbers are stored in the decimal type, with an internal representation that allows you to perform decimal math without rounding errors.

Once you have the number, you can format it using Decimal.ToString() for output purposes. This formatting is locale-specific; it respects your current culture setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文