3

In my simple Java written program I can't display the results of 0-10's square root and cube number without displaying one decimal point after? How do I get rid of this?

enter image description here

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignmenttwo;

/**
 *
 * @author JordanSimps
 */
public class AssignmentTwo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double Square;
        double Cube;

        for ( int Number = 0; Number <= 10; Number++ ) {
            Square = Number * Number;
            Cube = Number * Number * Number;
            System.out.printf("%-10s %-10s %-10s\n", "Number", "Square", "Cube");
            System.out.printf("%-10s %-10s %-10s\n", Number, Square, Cube);
        }
    }
}
Kai
  • 38,985
  • 14
  • 88
  • 103
JordanDevelop
  • 143
  • 1
  • 6
  • 21

4 Answers4

3

A very simple way to solve this is to change the type of Square and Cube to int instead of double. (But be sure your result will always be an Integer...)

You can also use a DecimalFormat object, it's pretty easy to understand. (It also looks more Java... What you did feel a bit like C.)

// this decimal format will display number after the dot only if there is.
// 1256.200 -> 1,256.2  whereas 152.0 -> 152
DecimalFormat df2 = new DecimalFormat( "###,##0.###" );
System.out.println("formated square : "+df2.format(Square));

EDIT to follow your example :

public static void main(String[] args) {
    double Square;
    double Cube;

    DecimalFormat df2 = new DecimalFormat( "###,##0.###" );

    for ( int Number = 0; Number <= 10; Number++ ) {
        Square = Number * Number;
        Cube = Number * Number * Number;
        System.out.println("Number \tSquare \tCube");
        System.out.println(Number + "\t" +  df2.format(Square) + "\t" + df2.format(Cube));
    }
}

Some Clarifications :

  • Using the first method you change the way the program STORES, your data (you choose integer or double (double precision floating point number)). In one case your data is stored as integer and therefore can't be a decimal value, in the other case it accept decimal value. Now it depends on what you want... If you are sure that no decimal value will be added to square and cube and that they won't overflow the integer type (value < 2^31) then you can use the first method

  • Using the second method you are just changing the way the computer DISPLAYS number. So your number is always stored as a double precision floating point so it can accept more values and your program will fit a bigger number of use cases. And when you want to display your number to the user, you tell the computer (using the DecimalFormat object) how you want to do it. In my example I said I want 3 number after the decimal point only if there is significant ones to display. You can have more details looking here

IMHO, I would advise the second method.

Marc
  • 2,631
  • 1
  • 12
  • 13
  • Could you show me an example of DecimalFormat with using my example so I get a better idea, please? – JordanDevelop Sep 02 '13 at 08:47
  • do you get a better idea now ? – Marc Sep 02 '13 at 08:53
  • Also please follow the [java naming conventions](http://geosoft.no/development/javastyle.html#Naming%20Conventions). In short use a lowercase letter to start your variable like this : myVariable, square, theCubeOfNumber... And start with an uppercase when declaring a type : MyClass, DecimalFormat. – Marc Sep 02 '13 at 08:58
  • I understand. I changed "double" for "Square", and "Cube" to "int" and it worked fine. What is the difference between using these two? – JordanDevelop Sep 02 '13 at 09:00
  • Well the first one you change the way the program STORES, your data (you choose integer or double (double precision floating point number)) in one case your data is stored as integer and therefore can't be a decimal value, in the other case it accept decimal value. Now it depends on what you want... If you are sure that no decimal value will be added to square and cube and that they won't overflow integer (2^31) then you can use the first method – Marc Sep 02 '13 at 09:06
  • But in the second method you are just changing the way the computer DISPLAYS number. So your number is always stored as a double precision floating point so it can accept more values so your program will fit a bigger number of use cases. But when you want to display it to the user, you tell the computer using the `DecimalFormat` object how you want to do it. In my example I said I want 3 number after the decimal point only if there is significant ones to display. You can have more details looking [here](http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) – Marc Sep 02 '13 at 09:10
  • @JordanSimps I edited my answer. Hope it answers your questions – Marc Sep 02 '13 at 09:16
2

Specify format as %-10.0f. Part after the decimal dot allows you to specify precision.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
0

Type cast your variables to Long

System.out.printf("%-10s %-10s %-10s\n", (long)Number, (long)Square, (long)Cube);

Indra Yadav
  • 600
  • 5
  • 22
-1

Use a BigDecimal set set the scale to 0

BigDecimal a = new BigDecimal ("2.1");
a.setScale(0, BigDecimal.ROUND_HALF_EVEN).toString() // => 2
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • wow a lot of downvotes on this thread with no comments. Why? – Scary Wombat Sep 02 '13 at 08:37
  • Well -1 isn't really "a lot"... But in my opinion it is absolutely needless to create an extra object for this. Because this is a very basic question the OP should not learn such bad style. – Kai Sep 02 '13 at 08:44
  • Well I will leave it to you to let the OP know all the other bad style that he is doing - OK? – Scary Wombat Sep 02 '13 at 08:47