Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-03-2008, 05:20 PM   #1
Solid State Member
 
Join Date: Apr 2007
Posts: 7
Default Java - How to have user choose # of decimal places

Hello. I am trying to figure out a way to have a user choose how many places of precision they want to be printed out of a number(a double), but I am having some trouble figuring out how to do this. I know you can use DecimalFormat or printf to choose how many places of precision to print out, but what I can't figure out is how to take user input and apply it to one of those methods. I was thinking of passing a variable(containing the user input) as an argument to the DecimalFormat object but that doesn't seem to work, so I'm curious how anyone else would do it. I already asked my teacher and he says he is unsure of how to do this in Java(this is the first time he's teaching this class in Java, as opposed to Pascal) and he says to just not do that part, but I would still like to know how to do it if anyone would be kind enough to enlighten me. I am a bit of a Java(and programming in general) newbie, I've taken an intro to programming using Java course, so i only have a couple of months of experience with Java and just can't seem to find a way to do this with what I've learned. Any help would be greatly appreciated!
__________________

staygold is offline   Reply With Quote
Old 03-03-2008, 05:26 PM   #2
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: Java - How to have user choose # of decimal places

I'm not sure because I don't do Java. But in my experience of programming, all the languages I have messed with have an If-then-else "function" if you will. Isn't there one in Java that you could apply?

edit: Java If-Then tutorial
__________________

JogaBonito1502 is offline   Reply With Quote
Old 03-03-2008, 05:37 PM   #3
Solid State Member
 
Join Date: Apr 2007
Posts: 7
Default Re: Java - How to have user choose # of decimal places

Java does indeed have the if-then-else function, but I don't quite see how that would be used to solve the problem... unless i make an if clause for all numbers between 1.7x10^-308 and 1.7x10^308... but that just seems like a bit too much work, haha. I'm sure its really simple, but i still can't figure it out... any further help?
staygold is offline   Reply With Quote
Old 03-03-2008, 05:45 PM   #4
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: Java - How to have user choose # of decimal places

Lol. I guess you want it to be able to deal with ALL cases then you're on your own or at least without me...

I originally thought you just wanted to deal with 5 or 6.

Well, it's kinda hard for me to find you a solution when all I got is I want to move the decimal place for this number of decimals. What you could do is input the number and the number of decimal places. Then count the numbers of characters from the back of the string up to your number of decimal places and put a period there. But in C++ the defining a variable as a double takes care of that. As the user can input 3.14143183 or whatever he wants to. So, yeah.
JogaBonito1502 is offline   Reply With Quote
Old 03-03-2008, 06:15 PM   #5
Solid State Member
 
Join Date: Apr 2007
Posts: 7
Default Re: Java - How to have user choose # of decimal places

sorry, I think the way I described the problem above it just confusing as hell, haha. Here's a better description: I have a program that takes 2 numbers from a user and calculates their sum and product. Then it asks how many decimal places to show, then print out the sum and product showing only that many decimal places (if the sum is 0.23546547665, and the user enters 3, then I want it to print 0.235). Here's the program that I have so far:
Code:
import java.util.Scanner; // Needed for the Scanner class.

public class Floating_And_Fixed 
{
	public static void main(String[] args) 
	{
		double[] values = new double[2]; // The number input by the user.
		double sum, product; // The sum and product of the two numbers.
		int precision; // The user defined number of decimal places to display.
		
		// Used to get input.
		Scanner keyboard = new Scanner(System.in);
		
		// Get the two numbers from the user.
		for (int index = 0; index <= 1; ++index) 
		{
			System.out.print("Please enter value number " + 
					(index + 1) + " : ");
			values[index] = keyboard.nextDouble();
		}
		
		// Calculate the sume and product.
		sum = calcSum(values[0], values[1]);
		product = calcProduct(values[0], values[1]);

		// Ask how many decimal places the user would like to be displayed.
		System.out.printf("\nHow many decimal places would you like to be displayed?\n\n");
		precision = keyboard.nextInt();

		// Consume the remaining newline.
		keyboard.nextLine();
		
		// Print the sum and product.
		System.out.printf("Sum:\n---------------------\nFixed-Point Notation: %f\n\n", sum);
		System.out.printf("Product:\n---------------------\nFixed-Point Notation: %f", product);
	}
	public static double calcSum(double value1, double value2)
	{
		double sum;
		
		sum = value1 + value2;
		
		return sum;
	}
	public static double calcProduct(double value1, double value2)
	{
		double product;
		
		product = value1 * value2;
		
		return product;
	}
}
staygold is offline   Reply With Quote
Old 03-03-2008, 06:29 PM   #6
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: Java - How to have user choose # of decimal places

Hum...the code is very similar to C++. Never examined Java before. I do understand it all. Thanks for the comments too (idk if you put that to help me or if it was already there.)

Here's what you can do. Have the sum and product be equaled into 2 different variables. An integer (a) and a double (b). This way you get the integer part of the number and the decimal. Then subtract the integer (a) from the double (b) and save this as a new variable (c). From here. What you can do is multiply this number by 10^n (n = number of decimal places). Save this new number as a integer (d). Make the a new double (e) equal this integer (d) then divide it by 10^n. This Doing this you eliminated the rest of the decimal places. Now add the new decimal (e) you have to the integer (a) found earlier. Save this as a new double (f). Return f. There probably is a much simpler way to do this and I just can't think of it right now (+ I think it's a little more advanced than this). I know this method sounds lengthy but it's not. so what you will have is this. (example just for sum)

int a, d;
double b, c, e, f;

sum = a;
sum = b;
c = b - a;
d = c * (10^n);
e = d;
f = e / (10^n);
return f;

**NOTE**
This does not take care of round. You will need an If-Then-Clause somewhere in here.
C++ allows you to equal an int to a double it just will round down to the next int, what I have shown you only applies if the previous is the same in Java.
JogaBonito1502 is offline   Reply With Quote
Old 03-03-2008, 06:51 PM   #7
Solid State Member
 
Join Date: Apr 2007
Posts: 7
Default Re: Java - How to have user choose # of decimal places

seems to make sense. I'll definitely try to apply this later tonight after class, and i'll let you know how it works out. Thanks a lot for your help!
staygold is offline   Reply With Quote
Old 03-03-2008, 06:53 PM   #8
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: Java - How to have user choose # of decimal places

No problem. Just glad to help. And glad people actually use this programming part of the forums lol. Everyone seems to be attracted to Social Lounge and the Software/Hardware part.
__________________

JogaBonito1502 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off



All times are GMT -5. The time now is 01:30 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0