Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

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

Avatar of undefined
Last Comment
Guy Hengel [angelIII / a3]
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about:
System.out.println(100.00/400.00); 

Open in new window

Avatar of sciuriware
sciuriware

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

;JOOP!
Avatar of Member_2_2394978

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Blurred text
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.
See Pricing Options
Start Free Trial
Avatar of sciuriware
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!
Avatar of sciuriware
sciuriware

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

;JOOP!
Avatar of Member_2_2394978

ASKER

Hmm, never knew that - Cheers.

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

Yes
Avatar of Member_2_2394978

ASKER

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" ? :)
Avatar of Member_2_2394978

ASKER

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

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

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

ASKER

sorry.
no problem, people...
just doing the remark, no action needed :)
Java
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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo