# 100/400 = 0.0!!

Hi,

So annoyingly my code is doing silly things!

For some reason System.out.println(400/100);
is outputing 0!!!!!

System.out.println(100/50); is fine, but as soon as the second part is higher than the first i get 0!!!!

This even happens if I do the some into a float first and then print the float!

I'm sure this hasn't always been happening!
James
Java

Last Comment
Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]

``````System.out.println(100.00/400.00);
``````
sciuriware

Did you ever learn about integers??????

;JOOP!

Heh, yes although integer, devided by an integer can make a float surely! - Or i'm guessing its rounded.

I'll have to do all my calculations in floats and then integer cast.
sciuriware

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
sciuriware

>>> I'll have to do all my calculations in floats and then integer cast.

No, you must realise what calculations you want.
And it is NOT rounding!!!!

;JOOP!
sciuriware

AS I said, in ALL programming languages, integer / integer NEVER yields float!

;JOOP!

Hmm, never knew that - Cheers.

So put a '.0' on the end, and its treated as a float right?
sciuriware

Yes

Thanks
I agree with sciuuriware:
>Heh, yes although integer, devided by an integer can make a float surely!
that would make the programming language doing things implicitely that you don't want actually.

>So put a '.0' on the end, and its treated as a float right?
float is the wrong term, actually. decimal would be better.
the main difference is how those data types store the numbers/digits internally.
in practice (apart from the scale), the main difference is the precision.
decimal numbers are "precise", float numbers are not.
interesting, that my very first comment was "ignored" ? :)

No; Sorry, I tried it and then the description of why was posted.
sciuriware

............... and a B on 50 points ................... doesn't exactly make my day.
sciuriware

angelIII, with 23 million points you should know the world good enough ...................

sorry.
no problem, people...
just doing the remark, no action needed :)
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY