Solved

C# Rounding Whole Numbers

Posted on 2013-02-07
2
366 Views
Last Modified: 2013-02-07
I have a question regarding c# and rounding.  Given the following equations c# seems to round them differently based on if a decimal is dictated in the whole number.  Is there any way to tell C# to output 7.5 regardless of the format of the code?

I am in the process of converting some legacy code and I don’t have the option of ensuring the static values are formatted with “.00”.

double test = 3000.00 / 400.00;
test = 7.5;

double test2 = 3000 / 400;
test2 = 7;

Open in new window

0
Comment
Question by:itcaptain
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 38865922
You're performing integer division in the second example. Integer division truncates the decimal. Either use a floating-point value (what you are doing in your first example), use an explicit cast:

double test2 = (double)3000 / 400;

...or use suffixes:

double test2 = 3000d / 400d;
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 38865939
In the 1st test, 3000.00 and 400.00 are seen as floating point numbers and floating point arithmetic is used resulting in a floating point number of 7.5 that gets assigned to the variable 'text'.

In the 2nd test, 3000 and 400 are seen as integer number and integer arithmetic is used resulting in an integer number of 7 that gets converted to a double that is then assigned to the variable 'test2'.

So to avoid the integer math, you have to make something a floating point number, and then the compiler should convert for you.

As an example, if you did the following:
double test3 = 3000.00 / 400;
test3 should have 7.5 because 400 will get converted to a floating point number to do floating point math on the already floating point value 3000.00.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now