Convert decimal to string without commas or dots
Asked Answered
A

8

12

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

Asphodel answered 23/7, 2013 at 12:28 Comment(3)
yourNumber.ToString().Replace(',','').Replace('.', '')Teresa
"but it didn't work as I expected" - then you didn't read the manual: "Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.". So "123,456.78".Remove(7) will yield 123,456.Poussette
That's what I found out as well. The hard way :)Asphodel
A
25

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. The bottom line, you don't know exactly what that string will always look like so it's safer this way.

Addam answered 23/7, 2013 at 12:31 Comment(4)
Whitespaces too (depending on the localization)Gusset
Cheers, it worked. Though Resharper suggested it writen like this: var amountString = string.Join("", amount.Where(char.IsDigit));Asphodel
+1 You could make it shorter ...amount.ToString().Where(Char.IsDigit) which is also more efficient.Amalgam
You could also do var amountString = string.Concat(amount.ToString().Where(char.IsDigit));Exponible
F
10

You say it's an amount, so I expect 2 digits after the decimal. 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:

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.

Festoonery answered 23/7, 2013 at 12:35 Comment(2)
Valid for Money and Double types, not Decimal typeMenstruate
It could works with decimal if you use Math.Round(value, 2). Example: long valueInCents = ((long)(Math.Round(value, 2) * 100));Nikko
B
3

You don't need casts, you don't need to know where the decimal is, and you certainly don't need 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.

Biographer answered 23/7, 2013 at 12:40 Comment(1)
I don't think there's an overload of Regex.Replace that takes a decimal as input argument. So, you'll be casting amount to a string before running the regex on it.Fortress
H
2
var amountString = amount.ToString().Replace(".","").Replace(",","");
Hypoderm answered 23/7, 2013 at 12:31 Comment(0)
S
0

I think this is what you're looking for:

value.ToString("D")

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

Sullins answered 23/7, 2013 at 12:28 Comment(1)
This throws FormatException seems Invalid for floating point typesWholehearted
I
0
var amount = 123456.78;
var amountString = amount.ToString().Replace(",", "").Replace(".","");
Ingraft answered 23/7, 2013 at 12:31 Comment(0)
D
0

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

Devilmaycare answered 23/7, 2013 at 12:32 Comment(0)
M
0
var amountString = amount.ToString().Replace(",",string.Empty).Replace(".",string.Empty);

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

Makalu answered 23/7, 2013 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.