4971 Shares

# How to remove extra numbers in decimal in java while loop program.?

Topic: How to write a while loop program
June 17, 2019 / By Crispin
Question: I am a freshman comp sci student writing a while loop program in the form of a matrix. How can I round my doubles to their tenths place value? I know it has something to do with the *100)/100; or something like that. Heres the code if it helps; public class Conversion{ public static void main (String [] args){ int kilograms; int pounds1; double kilo; double pounds; System.out.println("Kilogram \t\t Pounds \t\t Pounds \t\t\t\t Kilograms"); System.out.println("-----------------... kilograms = 1; pounds1 = 20; while(kilograms <= 199){ pounds = kilograms*2.2; kilo = pounds1/2.2; System.out.println(kilograms + "\t\t\t" + pounds + "\t\t\t" + pounds1 + "\t\t\t" + kilo); pounds1 += 5; kilograms += 2; } } } Thanks a lot dudevcc, but unfortunately I cant use this because my professor doesnt like for us to use things we have not learned yet. It has something to do with the "*100/100); thing but Im not near my book right now so Im not sure... But thanks again Dudevcc!

## Best Answers: How to remove extra numbers in decimal in java while loop program.?

Ashur | 2 days ago
Add this line after 'double pounds;' DecimalFormat df = new DecimalFormat("#,###.##"); This adds commas after a group of three numbers after the decimal and has two decimal places, if needed. Change the last println to: System.out.println(kilograms + "\t\t\t" + df.format(pounds) + "\t\t\t"+ pounds1 + "\t\t\t" + df.format(kilo)); I added enters so it didn't truncate the line. Hope this helps.
👍 132 | 👎 2
Did you like the answer? How to remove extra numbers in decimal in java while loop program.? Share with your friends

We found more questions related to the topic: How to write a while loop program

Originally Answered: Java Program which indents any specified java source code?
I would think of the code as a tree structure, for any given piece of code, it can be described based on it's context, or path from the root. The root in this case, would be the base level file, and would have 0 indentation. then you would go into your class, which would be a node, so indentation is 1 (tab or set of spaces), then into your function, so indentation is 2, then maybe into an if block, so indentation is 3, then maybe into a loop, so indentation is 4, then the loop ends, back at indentation 3, the if statement ends, back at indentation 2, and so on. I would probably write a class called for a node or something, which holds the data relevant to any given block, so it could hold if statements, or loops, or functions, whatever. Then I would probably have a class called context, which tracks the current path to the current piece of code, probably storing all of the nodes in an ArrayList or something, can tell you the context of the code, can tell you the indentation of the code (1 less than the size of the list), and so forth. I would probably use regular expressions to parse the code, by giving the context class a series of regular expressions that it can use to analyze a line of code, and determine if it is a new context, at which time it adds that a node to its list, to update the context, or back up a context, or whatever. Anyway, good luck.
Originally Answered: Java Program which indents any specified java source code?
What angel.white said. Plus, if you can make sure that the source file uses proper scoping (brackets around every scope) then you can use the brackets to help clue you in on the end of a given scope. Be aware, I have seen some very poorly written source code that did not have single statements on a single line, and would be very difficult to determine what code belongs with what scope programmatically.
Originally Answered: Java Program which indents any specified java source code?
Here's the same thing everyone else did, in C #include void printTable(int limit) { // change this to suit your needs if( (limit < 2) || (limit > 100) ) return; int row, column; for(row=0;row

Velma
because of the fact that your working with integers, I recommend taking a check out JOptionPane.ShowInputDialog(""); in basic terms make it a string and then parse it into an integer, makes it much less complicated to apply the counsel the customers places in.
👍 50 | 👎 -6

Originally Answered: How do you write a program that loops 10 times using a for loop?
Not 100% sure what your program is supposed to do, but I think it's something like this. Good luck! import java.util.Scanner; public class BullockJ_p4 { public static void main(String[] args) { final int MAX_VALUE = 10; int number = 0; int totalOdd = 0, totalEven = 0; Scanner keyboard = new Scanner(System.in); for (int i=0; i

If you have your own answer to the question how to write a while loop program, then you can write your own version, using the form below for an extended answer.