Solved

C# Rounding Whole Numbers

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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