Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Float  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey people, I am new to Java (and this forum) so being a newbie I'd like to ask a real newbie question; how do I round up a numeber in Float?
For example I have the number 2.339 but I'd like to display it as 2.3, how do I do this?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big NRG
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
Adam Kania
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Didn't notice the policy (and I'm just so damned used to the nick) I hope this is better!
Now could someone pls answer my question, better yet I'll ask another one;
If I have the total amount of seconds hwo can I display the amount of hours, minutes and seconds that makes up? Thanks!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a round() method in the Float class, but you can also easily create your own:
public float round(float f, int decimals)
{
int i = (int)(f * Math.pow(10, decimals) + 0.5);
return (float)i / Math.pow(10, decimals);
}
I'm coding this from memory, so I hope it's correct. First I multiply the number by a power of 10 to move the digit I want to round to the one's place. Then I add 0.5 before I truncate the number to an int. This ensures that the number is "rounded up" if the tenth's place is more than 0.5. If the tenth's place is less than 0.5, the number is rounded down. Then I convert the int back to a float by dividing by the appropriate power of 10.
HTH
Layne
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: you can use the / division and % modulo operators.
If I have, say, 5 % 2 it returns the remainder, which is 1. if it is perfectly divisible then it returns 0.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Math class has ceil(), floor(), and round() functions. Make sure you use the right one. You talk of "rounding up", but you want 2.339 to display as 2.3. That's either rounding down or rounding to nearest, but it isn't rounding up.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, take a look at the java.text.DecimalFormat class. You may want to play around with this simple program, to learn how formats work. It takes two command line arguments. The first is a number, the second is a format string:

[ September 24, 2002: Message edited by: Ron Newman ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The J2SE API Documentation that fully describes what Ron and others have mentioned can be found at http://java.sun.com/j2se/1.4.1/docs/api/ . Learning to reference the API documentation is a most valuable thing for a Java programmer to learn to do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!