简体   繁体   English

将十进制转换为不带逗号或点的字符串

[英]Convert decimal to string without commas or dots

In .NET, I need to convert a decimal amount (money) to a numbers-only string, ie: 123,456.78 -> 12345678在 .NET 中,我需要将十进制金额(钱)转换为仅数字字符串,即:123,456.78 -> 12345678

I thought我想

var dotPos = amount.ToString().LastIndexOf('.');
var amountString = amount.ToString().Remove(dotPos);

would solve my problem, or at least part of it, but it didn't work as I expected.会解决我的问题,或者至少是部分问题,但它没有按我预期的那样工作。 I'm trying to do this possibly without writing a lot of code and using something already designed for a similar purpose.我试图在不编写大量代码并使用已经为类似目的设计的东西的情况下做到这一点。

You could do it like this:你可以这样做:

var amountString = string.Join("", amount.Where(char.IsDigit));

Using the char.IsDigit method will protect you from other unknown symbols like $ and will also work with other currency formats.使用char.IsDigit方法可以保护您免受其他未知符号的影响,例如$并且还可以使用其他货币格式。 The bottom line, you don't know exactly what that string will always look like so it's safer this way.最重要的是,您并不确切知道该字符串将始终什么样子,因此这种方式更安全。

You say it's an amount, so I expect 2 digits after the decimal.你说这是一个数量,所以我希望小数点后有 2 位数字。 What about:关于什么:

 var amountstring = (amount * 100).ToString();

to get the cents-value without delimiters?获得没有分隔符的美分值?

Or maybe even或者甚至

var amountString = ((int)(amount * 100)).ToString();

to make sure no decimals remain.以确保没有小数保留。

EDIT编辑
If a cast to int isn't quite what you want (It would just ignore any extra decimals, like Math.Floor ), you can also use one of the Math.Round overloads, for instance:如果转换为int不是您想要的(它只会忽略任何额外的小数,例如Math.Floor ),您还可以使用Math.Round重载之一,例如:

var amountString = Math.Round(amount * 100, MidpointRounding.ToEven).ToString();

The MidpointRounding.ToEven (also known as "Banker's Rounding" ) will round a .5 value to the nearest even value, instead of always to the next higher value. MidpointRounding.ToEven (也称为“银行家的舍入” )会将 0.5 值舍入到最接近的数值,而不是总是舍入到下一个更高的值。

You don't need casts, you don't need to know where the decimal is, and you certainly don't need Linq.您不需要强制转换,不需要知道小数点在哪里,当然也不需要 Linq。 This is literally a textbook-case of Regular Expressions:这实际上是正则表达式的教科书案例:

Regex regx = new Regex("[^0-9]");
var amountString = regx.Replace(amount, "");

Couldn't be simpler.不能更简单。 And you can pass it strings with other odd monetary characters, or any character at all , and all you will get out is the decimal string.你还可以用其他货币奇怪的字符,或在所有任何字符传递给它的字符串,以及所有你会走出的是十进制字符串。

var amountString = amount.ToString().Replace(".","").Replace(",","");
var amount = 123456.78;
var amountString = amount.ToString().Replace(",", "").Replace(".","");

I would say this may help you: var res = amount.ToString().Replace(".", "").Replace(",", "");我会说这可能对你有帮助: var res = amount.ToString().Replace(".", "").Replace(",", ""); :) :)

var amountString = amount.ToString().Replace(",",string.Empty).Replace(".",string.Empty);

This will replace all the "," commas and "."这将替换所有的“,”逗号和“。” decimal from the amount.从金额小数点。

I think this is what you're looking for:我认为这就是你要找的:

value.ToString("D")

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM