andy
andy

Reputation: 595

how can remove decimal place after value in C#?

Let I have decimal value of 14.8447 and I need this value 8447 in string.

Can any one find any way for me using window Forms C#.

Upvotes: 0

Views: 5798

Answers (3)

Iori Yagami
Iori Yagami

Reputation: 134

Could be so:

decimal Numero = 8.24M;
string r = (Numero - Math.Floor(Numero)).ToString().Replace("0.", "");

Easy I guess;

Upvotes: 0

Uwe Keim
Uwe Keim

Reputation: 40736

You could use something like:

decimal d = 14.8447m;
string s = d.ToString(CultureInfo.InvariantCulture);
string t = s.Substring( s.IndexOf('.')+1);

(Although I think my code is a bit of a hack)

To make it a little bit more error-proof, you could write:

decimal d = 14.8447m;
string s = d.ToString(CultureInfo.InvariantCulture);

int index = s.IndexOf('.');
string t = index >= 0 && index + 1 < s.Length
       ? s.Substring(index + 1)
       : string.Empty;

This works well with e.g. the following numbers:

decimal d = 14m; // Returning empty string.
decimal d = .3m; // Returning "3".

Here is a short online version at Ideone.com.

Upvotes: 2

Sergey Podolsky
Sergey Podolsky

Reputation: 157

string.Format("{0:0.0000}", 14.8447m).Split(',', '.').Last()

Upvotes: 0

Related Questions