Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (607) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating percentages  (Read 1461 times) 0 Members and 1 Guest are viewing this topic.

Junior Newbie

 « Posted 2007-04-05 19:36:31 »

Hello, I've recently gotten interested in java for making games.  I like it because of it's similarities to c/c++  (I never did get into basic), and  it makes oop so much easier to me.  Plus the mobile market is still young enough indie developers can still take advantage of it.  Anyway I've been using "sams teach yourself java2" book to learn from and I'm going through all the excersises to get a better feel for the language.  I was trying to calculate percentages from scratch for one excersise, but the program would only give me back zero for the division.  Here's the problem broken down to the nitty-gritty:

public class Percent
{
public static void main(String[] arguments)
{
double percent;

//get the decimal value for 40%
percent = (40/100);

System.out.println(percent);
}
}

Am I missing something?  Is this kind of division taboo in java?  Any help would be greatly appreciated, thanks for your time.
oNyx

JGO Coder

Medals: 2

pixels! :x

 « Reply #1 - Posted 2007-04-05 19:43:24 »

bsh % print(40/100);
0
bsh % print(40.0/100.0);
0.4
---
bsh % print(100.0/35.0);
2.857142857142857
bsh % print(100/35);
2

See the pattern? If you want double values append ".0"... if you want floats append "f". With int there are no fractions.

CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

 « Reply #2 - Posted 2007-04-05 19:44:47 »

percent = (40.0/100.0);

40 and 100 were both integers.  So 40/100=0.4 as an integer is 0.  Then it is auto cast to a double.

You need the 40.0 and 100.0 to tell the compiler that you are working with doubles all the way.

Kova

Senior Devvie

 « Reply #3 - Posted 2007-04-05 19:48:47 »

as said, result of dividing integers is always an integer, I think it's the same in C and C++ so freshen up on basics...

Junior Newbie

 « Reply #4 - Posted 2007-04-06 06:57:27 »

Ah, thanks everyone.  It makes perfect sense.  I was using literals after all.  Kova, I don't think this was something that mattered in c, but I could be wrong.  Visual Studio lets you get away with a lot of things.
BloodRain

Junior Devvie

 « Reply #5 - Posted 2007-04-06 15:11:03 »

You are wrong.I even tested .
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (9 views) 2015-04-01 18:27:05 ags1 (25 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (48 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (33 views) 2015-03-13 17:50:51 Olo (40 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (61 views) 2015-03-10 11:36:42
 BurntPizza 21x LiquidNitrogen 21x KevinWorkman 18x EgonOlsen 17x theagentd 16x basil_ 16x Roquen 15x Varkas 12x wessles 11x 65K 11x Riven 9x Rayvolution 9x phu004 8x SHC 8x princec 8x Ashedragon 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org