• 13
• 15
• 19
• 27
• 9

# Converting from double to integer

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

## 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 on other sites
Well, I'm pretty sure this would do it:

static_cast<int>( Price )

##### 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 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 on other sites
Quote:
 Original post by ZahlmanIf 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 on other sites
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.

##### Share on other sites
Quote:
 Original post by MiserableConsidering 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 on other sites
Quote:
 Original post by MiserableConsidering 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 on other sites
Thank you all for your suggestions and insights. I really appreciate the help. Good programming to you all. Slider