Sign in to follow this  

Converting from double to integer

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would appreciate any thoughts on how I can convert from a double to an integer and not receive warnings every time I compile from Microsoft Visual C++ 6.0. I am working on a game that includes the purcahse or sale of various items (food, arms, etc) that may be priced using cents. However, the game only tracks dollars for players. Thus I am using Price (double) * Number (integer) to determine Cost (integer) which is subtracted from the player's funds. However, this results in warnings that "this conversion "=" may result in lost data." Now everything is working fine. The data is being rounded as desired, but I thought someone may know of a rounding function or some truncation method I haven't found. I hate leaving those warnings sitting out there even though things are working ok thus far. Thanks for any ideas, Slider

Share this post


Link to post
Share on other sites
ok

Cost = (int)(Price * (double)Number);

should do it, as long as you don't care that your decible points will be truncated off.

You can also disable the warning with a #pragma command, search msdn for it.

Share this post


Link to post
Share on other sites
If the only reason you're using doubles is to allow for two decimal places for "cents", then you don't really need to be using floating-point at all:


int unitCostInCents = 193; /* was 1.93 */
chargeUser(unitCostInCents * quantity / 100);
/* integer division of total cost in cents, to get dollars. */
/* Note this truncates; to round, add 50 cents before dividing. */
/* (To round up, add 99 cents before dividing.) */

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
If the only reason you're using doubles is to allow for two decimal places for "cents", then you don't really need to be using floating-point at all

Yeah, but floating-point is more intuitive, so it's better design. "BankBalance=976.43" is more intuitive than "BankBalance=97643". But 32-bit floats (the "float" type) is better to use than doubles.

To get the integer part of a double, but still using a double, do this:

double Money = 976.32;
double Dollars = floor(Money); //Dollars == 976.00

If you're using 32-bit floats, use floorf instead:

float Money = 976.32f;
float Dollars = floorf(Money); //Dollars == 976.00

Share this post


Link to post
Share on other sites
Quote:
Original post by Miserable
Considering that floating-point math is inherently imprecise, I would think that a banking application is the last place you would want to use it.


Well... Maybe banking is still more imprecise than our floating point :) (just kidding).

To the OP : these warning are only hints. The compiler tells you : "hey, I found that you may loose some data when applying this conversion". Therefore I do not think that hiding this warning with a #pragma is a good idea - because it can help you to catch weird errors.

When you are sure of what you do, just cast your value instead of relying on the automatic cast that the compiler need to do. Vodka's version is good but perhaps a little to intrusive. I think dede's version is better/simpler to write.

Share this post


Link to post
Share on other sites
Quote:
Original post by Miserable
Considering that floating-point math is inherently imprecise, I would think that a banking application is the last place you would want to use it.

Well doubles are imprecise as well, just less so. But I can't see how 32-bit precision is too low for dealing with the addition and multiplication of dollars and cents. Physics, sure, but not money.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Vodka's version is good but perhaps a little too intrusive. I think dede's version is better/simpler to write.
Using C-style casts in C++ is actually not a good practice for various reasons, dest = static_cast<dest_t>(source) should always be used over dest = (dest_t)source.

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this