Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating percentages  (Read 2165 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

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (555 views) 2018-03-24 14:43:53 Solater (94 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (338 views) 2018-03-02 22:15:33 buddyBro (998 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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