The Artima Developer Community
Sponsored Link

Java Answers Forum
Rounding In Java

2 replies on 1 page. Most recent reply: Sep 20, 2003 11:51 PM by FredrikN

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
N. Vu

Posts: 1
Nickname: autonomic
Registered: Sep, 2003

Rounding In Java Posted: Sep 20, 2003 5:50 PM
Reply to this message Reply
Advertisement
Hi,

I'm a bit of a Java newbie and I was wondering whether you can round numbers in Java.

Like, let's say I have this double 4.7688623421, is there a way I could round this to 2 decimal places so I can output it to the screen?

Thanks a lot.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Rounding In Java Posted: Sep 20, 2003 6:41 PM
Reply to this message Reply
Did you google before posting your question?

http://www.dickinson.edu/~braught/courses/cs132s01/classes/code/Rounding.src.html

FredrikN

Posts: 22
Nickname: fredrikn
Registered: Jul, 2003

Re: Rounding In Java Posted: Sep 20, 2003 11:51 PM
Reply to this message Reply
Or just use the NumberFormat
The foolowing code will display 4.77

import java.text.*;
public class Driver
{
 
	public static void main(String[] args)
	{
		float f = 4.7688623421f;
	
		System.out.println( new DecimalFormat("0.##").format(f)   );
	}
 
}

Flat View: This topic has 2 replies on 1 page
Topic: URGENT::: Error  java.lang.NoClassDefFoundError: org/apache/commons/beanuti Previous Topic   Next Topic Topic: How can I select a particular row in a JDBTable ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use