By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,182 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,182 IT Pros & Developers. It's quick & easy.

how to fix decimal places?

P: n/a
Ron
Greetings,

int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));

both yield 0.166666666666667

how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ron
I found this so far, is there a more correct method?

int i = 1, j = 6;
double k = (double)i/j;
decimal rounded = decimal.Round((decimal)k, 2);
Console.WriteLine(rounded.ToString());

yields 0.17

-----Original Message-----
Greetings,

int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));

both yield 0.166666666666667

how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron
.

Nov 16 '05 #2

P: n/a
Hi Ron! :O)

you should take a look at String.Format() method's help.. it is not the same
as the Format$() method of VB.

ToString() is the method you were looking for (sooo close ;O):
//***
Console.WriteLine(((double)5.2345345).ToString("0. 00"));
Console.ReadLine();
//***

--
Best Regards
Yanick
"Ron" <an*******@discussions.microsoft.com> a écrit dans le message de
news:15****************************@phx.gbl...
Greetings,

int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));

both yield 0.166666666666667

how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron

Nov 16 '05 #3

P: n/a
the decimal.Round() method (and some others i guess) uses a algorithm named
"Banker's Rounding" which might not give you the result you expect..

ex :
//***
Console.WriteLine(decimal.Round(((decimal)0.5), 0).ToString());
Console.WriteLine(decimal.Round(((decimal)1.5), 0).ToString());
Console.WriteLine(decimal.Round(((decimal)2.5), 0).ToString());
Console.ReadLine();
//***

returns :
0
2
2

see this link for more information about this topic
http://support.microsoft.com/kb/196652/en-us
besides, as you might know, there is a huge difference between Rounding a
value (loosing precision) and Formatting a value (showing a value in a human
readable format), thus ToString() is the way go.
//***
Console.WriteLine(((double)0.5).ToString("0"));
Console.WriteLine(((double)1.5).ToString("0"));
Console.WriteLine(((double)2.5).ToString("0"));
Console.ReadLine();
//***

returns :
1
2
3

--
Best Regards
Yanick
"Ron" <an*******@discussions.microsoft.com> a écrit dans le message de
news:0b****************************@phx.gbl...
I found this so far, is there a more correct method?

int i = 1, j = 6;
double k = (double)i/j;
decimal rounded = decimal.Round((decimal)k, 2);
Console.WriteLine(rounded.ToString());

yields 0.17

-----Original Message-----
Greetings,

int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));

both yield 0.166666666666667

how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron
.

Nov 16 '05 #4

P: n/a
Ron
Yes, thank you. That is what I was looking for.

Thanks.

-----Original Message-----
Hi Ron! :O)

you should take a look at String.Format() method's help.. it is not the sameas the Format$() method of VB.

ToString() is the method you were looking for (sooo close ;O)://***
Console.WriteLine(((double)5.2345345).ToString("0 .00"));
Console.ReadLine();
//***

--
Best Regards
Yanick
"Ron" <an*******@discussions.microsoft.com> a écrit dans le message denews:15****************************@phx.gbl...
Greetings,

int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));

both yield 0.166666666666667

how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron

.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.