Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2008, 04:24 PM
Member
 
Join Date: Oct 2008
Posts: 39
Rep Power: 0
arnab321 is on a distinguished road
Angry decimal calculations?
hi,
when i try this: float a=p.getDuration()/1000000;
System.out.println("a="+a+" actual="+p.getDuration());

p.getDuration() is a number like 1791800
so, a is expected to be 1.7918.
but why does is come as 1.0?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-17-2008, 04:46 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Is that getDuration() return an int value? In that case you have to cast it as a float.

Code:
float a = (float)p.getDuration()/1000000;
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-17-2008, 05:16 PM
Member
 
Join Date: Oct 2008
Posts: 39
Rep Power: 0
arnab321 is on a distinguished road
Default
tried that just now. it still returns 1.0. getDuration returns a long
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-18-2008, 08:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Can you show your code here to see?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-19-2008, 01:40 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
you need something like:

Code:
float a = (float)(p.getDuration()/1000000.0);
Your use of an integer in the denominator causes the division of "p.getDuration()" to be done as an integer.

Floating an integer is still an integer
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-19-2008, 04:36 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Or yes lol, thanks for the pointing my mistake there. I just code in the message box here.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
numbers with two decimal places little_polarbear New To Java 8 08-28-2008 12:04 AM
Why my program cannot calculate the decimal value? pearllymary78 New To Java 4 06-23-2008 01:52 AM
Java calculator decimal cart1443 New To Java 2 04-16-2008 02:19 PM
Capping decimal places Rageagainst20 New To Java 1 12-20-2007 10:28 PM
round to two decimal places javaMike New To Java 1 11-26-2007 10:46 PM


All times are GMT +2. The time now is 12:20 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org