Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-07-2008, 10:04 PM   #1
Registered: Apr 2007
Location: 中国上海
Distribution: openSUSE 11.3
Posts: 371

Rep: Reputation: 40
use decimals in java?

I want to write a small program that averages some numbers together, but to do that, I can do the adding part, but I can't get the program to output a decimal. I f i use java.lang.Math.round(total,2) then i get this error message:

brian@brian-linux:~/Documents/java> javac cannot find symbol
symbol  : method Round(double,int)
location: class java.lang.Math
double round = java.lang.Math.round(total,2);
1 error
did I do something wrong?


Old 03-08-2008, 12:08 AM   #2
LQ Newbie
Registered: Mar 2008
Posts: 9

Rep: Reputation: 0
The Math.round method only takes one argument. It does not allow you to truncate based on decimal places. See the API reference:

What you want can be accomplished like this though:

public class Sum{
		Usage: java Sum <number>...
	public static void main(String[] args){
		float sum = 0;
		for(String str : args){
			sum += Float.parseFloat(str);			
		System.out.printf("%02.2f\n", sum);
You could also surround the Float.parseFloat to catch the NumberFormatException if an invalid value is passed.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Java news met with cautious optimism in free Java community LXer Syndicated Linux News 0 11-14-2006 10:21 PM
how do I use "format" with "pipe" to format decimals Joseph Schiller Programming 1 12-23-2005 12:30 PM
how to compare decimals linuxboy69 Linux - Software 2 08-16-2004 02:43 PM
Can I use a lot of decimals in my C program? Golradir Programming 14 07-04-2004 12:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration