Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Rounding Whole Numbers

Posted on 2013-02-07
2
Medium Priority
?
376 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
[X]
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
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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