我将如何在C#中的空间分隔千 [英] How would I separate thousands with space in C#

查看:107
本文介绍了我将如何在C#中的空间分隔千的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有我不得不格式,以便使每千名应该用空格分隔以下十进制数字:

Assume I have the following decimal number that I have to format so that the every thousand should be separated with a space:

 897.11 to 897.11
 1897.11 to 1 897.11
 12897.11 to 12 897.11
 123897.11 to 123 897.11

我曾尝试Decimal.ToString的(0 000.00)。虽然这工作得很好时数为1897.11。但是,当它的897.11,我得到0 897.11。

I have tried Decimal.ToString("0 000.00"). Although this works pretty well when the number is 1897.11. But when it's 897.11 I get 0 897.11.

推荐答案

在传递一个自定义的 的NumberFormatInfo 一个自定义的 NumberGroupSeparator 财产和使用的 #,#格式来告诉它做的组数。本示例使用固定区域性的数字格式为基础。

Pass in a custom NumberFormatInfo with a custom NumberGroupSeparator property, and use the #,# format to tell it to do number groups. This example uses the invariant culture's number format as its basis.

var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
string formatted = 1234897.11m.ToString("#,0.00", nfi); // "1 234 897.11"

这篇关于我将如何在C#中的空间分隔千的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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