3

If I have a simple decimal in C#, and I want to ToString it without any dots, commas or decimals in any culture - how do i do it?

I tried the ToString("N0") with invariantculture which removes decimals, but it adds "." in my language.

What is the best way to do this?

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182

3 Answers3

5

You haven't clarified in the comments yet, but I suspect you want something like this?

3.14 -> 3

5.34543543 -> 5

If that is correct then you simply need to cast your decimal to an int which would strip the precision off and give you just the whole number.

string someString = ((int)someDecimal).ToString();

If you find yourself repeating this logic often you can make a simple extension method to keep it DRY:

public static class DecimalExtensions
{
    public static string ToIntString(this decimal input)
    {
        return ((int)input).ToString();
    }
}

And simply call it like so:

string someString = someDecimal.ToIntString();

Simple fiddle here

EDIT

@HansPassant actually gave the simplest solution in the comments and I feel silly for not remembering:

string someString = someDecimal.ToString("F0", System.Globalization.CultureInfo.InvariantCulture);

This is a much better answer and if Hans posts an answer I suggest marking theirs as the solution.

Community
  • 1
  • 1
maccettura
  • 10,514
  • 3
  • 28
  • 35
  • 1
    Great example that when your mind is caught in some culture/tostring stuff, the simplest solution is just obvious and easy. Thanks for showing how stupid I was - I will mark as answer in 6 minutes ;-) – Lars Holdgaard Mar 30 '18 at 14:17
  • @LarsHoldgaard I just edited and added an extension method example too if you find it useful. – maccettura Mar 30 '18 at 14:20
  • Agreed with the edit. May not be the most readable, but definitely the most succinct, and you don't have to do any additional method calls. – Daxtron2 Mar 30 '18 at 14:27
  • 1
    Make sure format does not use group separators - I never can remember if `ToString` could produce something like `1,000,000`... I'd personally explicitly specify `InvariantCulture` to be sure... – Alexei Levenkov Mar 30 '18 at 14:51
1

In addition to maccettura's answer which will only work for numbers between (2^31)-1 and -(2^31)-1. You can use:

decimal.Truncate(decimalToTruncate)

which should work for all values that a decimal can store.

Simple fiddle by maccettura

Decimal.Truncate on MSDN

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
  • Nice, didnt know about that method – maccettura Mar 30 '18 at 14:21
  • No problem. I don't know if it's any better than just doing it your way or not, but it's always good to know that there *are* multiple ways of doing things. – Daxtron2 Mar 30 '18 at 14:22
  • I think your way is much simpler and accounts for any number in the decimal range. I upvoted. I also made a simple fiddle for it [here](https://dotnetfiddle.net/ZH4xRH) if you want to include it in your answer. – maccettura Mar 30 '18 at 14:23
0

The simple way is just use Convert.ToInt32("Your Value") instead of .ToString().

So

string result= Convert.ToInt32("3.14").ToString();

It will give you "3" as result.

jkmehmi
  • 66
  • 1
  • 8