Reputation: 595
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
Reputation: 134
Could be so:
decimal Numero = 8.24M;
string r = (Numero - Math.Floor(Numero)).ToString().Replace("0.", "");
Easy I guess;
Upvotes: 0
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
Reputation: 157
string.Format("{0:0.0000}", 14.8447m).Split(',', '.').Last()
Upvotes: 0