This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

just basic division

 
darius vaas
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's simple! i can't do division!

if i try: float x = 5/2;
the result is: 2.0

how will i get: 2.5
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Both 5 and 2 are integers, therefore the '/' operator assumes you want integer division. Try 5.0/2.0 .
 
darius vaas
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's! (yes i'm complete newby)

actualy that didn't work as i derived two numbers from dates and i had to divide them. but with your answer in mind i recognized i have to change the type. so i did: double z = (double) x / (double) y

again, thank's for the (swift) answer
 
Campbell Ritchie
Sheriff
Pie
Posts: 47253
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try "x = 0.0f + 5 / 2;" See whether that works
 
darius vaas
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, that's not working. only double does the trick. but there is another problem: 79.39767282683093 is a bit toooo long for me, how can it be truncated to let's say 79.397?
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot really round a number. If you want to display it a screen (paper, etc), you have to use NumberFormat class. Run this code to see what it does:Explore API for other functionality/subclasses of this class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47253
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try "x = 1.0 * 2 / 5;" then.
Try "System.out.printf("My number is %.3f", z);"

Look up the java.util.Formatter class, and find the methods Formatter.format, String.format, PrintStream.format and PrintStream.printf, which all take the same sort of arguments.
 
darius vaas
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem solved!
thank's everyone for help and i'm quite sure i will need your help again soon. 'till then be well!
[ May 07, 2007: Message edited by: darius vaas ]
 
Casey Buresh
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use DecimalFormat. this will limit the number of decimal places your answer it and if you haven't already change float to double. Here is some example code where I did almost what it sounds like what you where trying to do. This code divides two numbers and uses decimal format to control the decimals.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic