Solved

C# Rounding Whole Numbers

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using sample Autorize.net c# simple example 1 54
Check only one toolstripmenu item 12 62
aspx ascx, c# 7 43
Put File 6 36
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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