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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17810211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:31:33  来源:igfitidea点击:

Convert decimal to string without commas or dots

c#.net

提问by Fat Shogun

In .NET, I need to convert a decimal amount (money) to a numbers-only string, i.e: 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.

会解决我的问题,或者至少是部分问题,但它没有按我预期的那样工作。我试图在不编写大量代码并使用已经为类似目的设计的东西的情况下做到这一点。

采纳答案by Mike Perrenoud

You could do it like this:

你可以这样做:

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

Using the char.IsDigitmethod will protect you from other unknown symbols like $and will also work with other currency formats. The bottom line, you don't know exactlywhat that string will always look like so it's safer this way.

使用该char.IsDigit方法可以保护您免受其他未知符号的影响,$并且还可以使用其他货币格式。最重要的是,您并不确切知道该字符串将始终什么样子,因此这种方式更安全。

回答by Jonesopolis

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

回答by dazedandconfused

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

回答by dotixx

I would say this may help you: var res = amount.ToString().Replace(".", "").Replace(",", "");:)

我会说这可能对你有帮助var res = amount.ToString().Replace(".", "").Replace(",", "");:)

回答by Aneesh Mohan

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

This will replace all the "," commas and "." decimal from the amount.

这将替换所有的“,”逗号和“。” 从金额小数点。

回答by Hans Ke?ing

You say it's an amount, so I expect 2 digits after the decimal. What about:

你说这是一个数量,所以我希望小数点后有 2 位数字。关于什么:

 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.

以确保没有小数保留。

回答by Pat Lillis

You don't need casts, you don't need to know where the decimal is, and you certainlydon't need Linq. This is literally a textbook-case of Regular Expressions:

您不需要强制转换,不需要知道小数点在哪里,当然也不需要 Linq。这实际上是正则表达式的教科书案例:

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 allyou will get out is the decimal string.

不能更简单。你还可以用其他货币奇怪的字符,或者通过它串在所有的任何字符,以及所有你会走出的是十进制字符串。

回答by Simón

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