Greetings,
int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLi ne(k.ToString() );
Console.WriteLi ne(string.Forma t(k.ToString(), "0.00"));
both yield 0.1666666666666 67
how can I make it yield 0.17? I know there is some
method, I just can't think of it.
Thanks,
Ron 4 50323
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.WriteLi ne(rounded.ToSt ring());
yields 0.17 -----Original Message----- Greetings,
int i = 1, j = 6; double k = (double)i/j; Console.WriteL ine(k.ToString( )); Console.WriteL ine(string.Form at(k.ToString() , "0.00"));
both yield 0.1666666666666 67
how can I make it yield 0.17? I know there is some method, I just can't think of it.
Thanks, Ron .
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.WriteLi ne(((double)5.2 345345).ToStrin g("0.00"));
Console.ReadLin e();
//***
--
Best Regards
Yanick
"Ron" <an*******@disc ussions.microso ft.com> a écrit dans le message de
news:15******** *************** *****@phx.gbl.. . Greetings,
int i = 1, j = 6; double k = (double)i/j; Console.WriteLi ne(k.ToString() ); Console.WriteLi ne(string.Forma t(k.ToString(), "0.00"));
both yield 0.1666666666666 67
how can I make it yield 0.17? I know there is some method, I just can't think of it.
Thanks, Ron
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.WriteLi ne(decimal.Roun d(((decimal)0.5 ), 0).ToString());
Console.WriteLi ne(decimal.Roun d(((decimal)1.5 ), 0).ToString());
Console.WriteLi ne(decimal.Roun d(((decimal)2.5 ), 0).ToString());
Console.ReadLin e();
//***
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.WriteLi ne(((double)0.5 ).ToString("0") );
Console.WriteLi ne(((double)1.5 ).ToString("0") );
Console.WriteLi ne(((double)2.5 ).ToString("0") );
Console.ReadLin e();
//***
returns :
1
2
3
--
Best Regards
Yanick
"Ron" <an*******@disc ussions.microso ft.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.WriteLi ne(rounded.ToSt ring());
yields 0.17
-----Original Message----- Greetings,
int i = 1, j = 6; double k = (double)i/j; Console.WriteL ine(k.ToString( )); Console.WriteL ine(string.Form at(k.ToString() , "0.00"));
both yield 0.1666666666666 67
how can I make it yield 0.17? I know there is some method, I just can't think of it.
Thanks, 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.WriteL ine(((double)5. 2345345).ToStri ng("0.00")); Console.ReadLi ne(); //***
-- Best Regards Yanick "Ron" <an*******@disc ussions.microso ft.com> a écrit dans
le message denews:15******* *************** ******@phx.gbl. .. Greetings,
int i = 1, j = 6; double k = (double)i/j; Console.WriteLi ne(k.ToString() ); Console.WriteLi ne(string.Forma t(k.ToString(), "0.00"));
both yield 0.1666666666666 67
how can I make it yield 0.17? I know there is some method, I just can't think of it.
Thanks, Ron
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the number has 2 decimal places.
Any help would be great.
Regards,
Brent
|
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 function that does it by converting the double to a string. Is there a better way
public static int CountDecimalPlaces(double double1)
return double1.ToString().Substring(double1.ToString().IndexOf(".")+1).Length
Thank yo
|
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 Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function but
it only truncates to whole numbers. how can I do the truncation?
Thanks,
G
|
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 Tables Collection, the Tables
Fields Collection, and then the Field Properties Collection but I do
not see what I need. I don't want to change the setting. I just want
to make a list of the decimal place setting for each "Single" field in
|
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 starting a new one]
I'd still like to finish this rounding mess. As a startup lemma we can
take that VK is the worst programmer of all times and places: let's
move from here forward please.
The usability of any program depends on exact behavior...
| |
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
field.
Markup:(-)/
which is to formatted as Percent with 0 Decimal places.
When I click into the Format box is am offered
|
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
|
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 decimal places, when click on the field. I do not want to modify the table structures.
|
by: David |
last post by:
Any ideas why this statement:
mBalanceChange = Double.Parse(strCurrentBalance,
Globalization.NumberStyles.AllowThousands Or
Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble
would leave me with a double containing over 10 decimal places when
strCurrentBalance was a string containing a double with two decimal places
and mBalanceDouble contained only two decimal places to begin with?
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I format a Number as a String with
exactly 2 decimal places?
-----------------------------------------------------------------------
When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?
Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly. See the section on "simple decimal arithmetic"
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |