473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Isssue with converting double to int

Hi,

I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:

int requiredShares = (int)(1230.98);

The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.

In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.

Anyone can tell me if there's any .NET utility that does what I want?
Oct 10 '08 #1
4 4135
"Curious" <fi********@yahoo.comwrote in message
news:39**********************************@u57g2000 hsf.googlegroups.com...
Hi,

I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:

int requiredShares = (int)(1230.98);

The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.

In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.

Anyone can tell me if there's any .NET utility that does what I want?

int rounded = (int)System.Math.Round(1230.98, 0);
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

Oct 10 '08 #2
Curious wrote:
I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:

int requiredShares = (int)(1230.98);

The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.
Conversion by truncation is a time-honored tradition in C-derived languages,
and you're a blasphemer for suggesting it's "not right". Just so you know. :-)
In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.
What do you want to round to when it's exactly 0.5, then?

Use Math.Round(), *then* convert the result. Math.Round() also has an
overload for specifying how to round numbers that are exactly between two
integers.

--
J.
Oct 10 '08 #3
int rounded = (int)System.Math.Round(1230.98, 0);
Thanks Mark! This works!
Oct 13 '08 #4
Jeroen:
What do you want to round to when it's exactly 0.5, then?
I don't have to be concerned about this situation.
Use Math.Round(), *then* convert the result. Math.Round() also has an
overload for specifying how to round numbers that are exactly between two
integers.
Thanks for letting me know. It works!
Oct 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Aaron | last post by:
I get the following message if I try to install to a directory that is not the default... A portion of the folder path '' is invalid. It is either empty or exceeds the length allowed by the...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.