Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Rounding Whole Numbers

Posted on 2013-02-07
2
372 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 75

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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