473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

how to fix decimal places?

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
4 50189
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
3
by: android | last post by:
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out the number of decimal places I have written a...
5
by: GaryB | last post by:
I have a database field that is the product of a multiply in a sql statement and it sometimes has 5 or more decimal places. I want to truncate beyond the 2nd decimal place(###.##). I figured a...
4
by: Hank | last post by:
Hello, I'm running Access 2000. I would like to know if its possible to use VBA code to determine how many decimal places are specified for each table field. Currently I loop throught the...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
2
by: hunslair | last post by:
This is a really basic question. I am taking a teach yourself beginners course on Access 2003 and have run into a road block on one practice. In a select query, I have created a simple calculation...
5
by: =?Utf-8?B?S3VuYWwgUGF0ZWw=?= | last post by:
I have a DataTable with Column DataType as Double I just want to show data in C# datagrid with two decimal places which property do I set for DataColum of DataTable and How? Thanks
3
by: jamesnkk | last post by:
I have a currency field Total_Amount in the form,it display at 2 decimal places, but if i click on the field, it display 4 decimal places. How do I not show 4 decimal places but only show 2...
3
by: David | last post by:
Any ideas why this statement: mBalanceChange = Double.Parse(strCurrentBalance, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble ...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.