简体   繁体   中英

I can't seem to get rid of the decimals after my square root and cube print?

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?

在此输入图像描述

/*
 * 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);
        }
    }
}

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.

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

Type cast your variables to Long

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

Use a BigDecimal set set the scale to 0

BigDecimal a = new BigDecimal ("2.1");
a.setScale(0, BigDecimal.ROUND_HALF_EVEN).toString() // => 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM