How to display only numbers after comma in c#
-
i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?
Heres a simple way:
static void Main(string[] args) { double x = 33.452; string number = x.ToString(); char[] sperator = { '.' }; string[] seperate = number.Split(sperator); double xSplit = Convert.ToDouble(seperate[1]); Console.WriteLine(xSplit); }
That will output 452 to the console. There might be an easier way though I'm not sure. Hope that helps ;P
-
i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?
-
Probably and eaiser way...But this works. double d = 3.14; string s = d.ToString(); char[] delimiter = { '.' }; string[] a = s.Split(delimiter); Console.WriteLine(a[1]); gl-Paul
-
Heres a simple way:
static void Main(string[] args) { double x = 33.452; string number = x.ToString(); char[] sperator = { '.' }; string[] seperate = number.Split(sperator); double xSplit = Convert.ToDouble(seperate[1]); Console.WriteLine(xSplit); }
That will output 452 to the console. There might be an easier way though I'm not sure. Hope that helps ;P
-
didn't see your post...From your '???' do you not realize how two people can post within an 8 minute perion?
-
I'm not insulting you I just thought it was kind of funny that we said the exact same thing.
-
i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?
Unfortunately the other solutions will fail under other cultures. Do the following instead:
double x = 33.452;
Console.WriteLine(x % Math.Floor(x)); -
i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?
This works for any culture. It also handles the case where there is no fractional digits, instead of causing an exception.
double x = 33.452;
string number = x.ToString(CultureInfo.InvariantCulture);
int pos = number.IndexOf('.');
if (pos == -1) {
Controle.WriteLine();
} else {
Console.WriteLine(number.Substring(pos + 1));
}--- b { font-weight: normal; }