Error: Possible lossy conversion from double to int error. int birthrate = 1.0 / 7.0 ^

It does this for birthrate, deathrate, and immigrationrate. I've changed everything from "int" to "double" and it compiles, but then when executed I get decimals where I don't want them.

You are watching: Possible lossy conversion from long to int


*

If you want to explicitly tell the compiler that you want to convert the numbers to integers, you should cast them as such. Example: int x = (int)(10.0/2.0) will make x have a value of 5.

Do note, however, that in the case of int birthrate, the operation 1.0/7.0 will simply be 0 when cast to an int.


Doing that fixed my errors, but when the 5 statements are printed after I enter a population, it just gives me whatever I entered 5 times in a row.

I'm confused on why 1.0/7.0 is 0 when an int, can you go further than that? I'm sorry, pretty new to programming.


Also, it doesn't necessarily have to be int, but whenever I use anything else it gives me more errors.


Small hint: jsfiddle is for JavaScript, ideone is for Java. - Not the same language.

You need to review your data types:

int and long are for whole numbers only.

float and double are for decimal numbers

So, when you write

int birthrate = 1.0/7.0;Java will complain that it is a lossy conversion because you lose all the decimals and the result will just simply be 0 (because 7 fits in 1 exactly 0 times - integer division - primary school maths).

What you want to store your birthrate in is a double:

double birthrate = 1.0/7.0;will yield the correct result of 0,142857142857142 (rounded after the 15th digit due to double precision).

I've changed everything from "int" to "double" and it compiles, but then when executed I get decimals where I don't want them.

Which just means that you have to perform a cast to int where you want no decimals.

Do all the math in doubles and finally, when displaying, cast to int.


3
Share
ReportSave
level 2
Op · 6y

Thank you, apologies for the jsfiddle mix up, never used either before. To perform a cast to int do I simply put that in the print statement or beforehand? Your explanation makes perfect sense, I'm just not sure where I put the cast to int, or how to do that.


1
Share
ReportSave
Continue this thread 


level 1
· 6y
I'm not understanding what you're trying to do. Do all your arithmetic with floating point numbers and then if you want to display whole numbers to the user then cast (or format) them at the end.

See more: Galatians 1:3-5 - Galatians 1:3 Kjv


2
Share
ReportSave
More posts from the learnprogramming community
Continue browsing in r/learnprogramming


r/learnprogramming
A subsommos.net for all questions related to programming in any language.
2.4m
Members


1.3k

Online


Created Sep 24, 2009
Join
*
*
*
*

*

Top posts january 26th 2016Top posts of january, 2016Top posts 2016
helpsommos.net coinssommos.net premiumsommos.net gifts
aboutcareerspressadvertiseblogTermsContent policyPrivacy policyMod policy
Back to Top