Java basic arithmetic and data type

时间:2016-04-04 17:19:25

标签: java math

Silly question but can't find the answer.

    double divider = 1000;

    List<Long> listLong = new ArrayList<>();
    listLong.add(1500L);
    listLong.add(8000L);

    for (Long val : listLong)
    {
        System.out.println((val/ divider));
    }

Gives me

1.5
8.0

and I want

1.5
8

distance and distance2 are Long and can't be changed. divider has to be the same thing for both cases.

ANWSER:

Thanks to KevinO

    DecimalFormat df = new DecimalFormat("#.##");

    for (Long val : listLong)
    {
        System.out.println(df.format(val/ divider));
    }

5 个答案:

答案 0 :(得分:1)

Try to use DecimalFormat like this:

  DecimalFormat df = new DecimalFormat("###.#");
  System.out.println(df.format(distance2 / divider));

答案 1 :(得分:1)

You can do that by using a DecimalFormat.

Here's an example of what that would look like:

DecimalFormat df = new DecimalFormat("0");
df.setMaximumFractionDigits(100);

System.out.println(df.format(1500L/1000D)); //Output: 1.5
System.out.println(df.format(8000L/1000D)); //Output: 8
System.out.println(df.format(1234L/1000D)); //Output: 1.234

This shows only the necessary digits, and removes trailing zeroes.

The (possible) advantage of this over new DecimalFormat("###.#") is that it includes all the trailing digits, rather than rounding to 1 decimal place.

DecimalFormat df = new DecimalFormat("###.#");

System.out.println(df.format(1500L/1000D)); //Output: 1.5
System.out.println(df.format(8000L/1000D)); //Output: 8
System.out.println(df.format(1234L/1000D)); //Output: 1.2

答案 2 :(得分:1)

You can use DecimalFormat to accomplish what you need. Going off of your updated question:

    double divider = 1000;

    List<Long> listLong = new ArrayList<>();
    listLong.add(1500L);
    listLong.add(8000L);
    DecimalFormat df = new DecimalFormat("###.#");

    for (Long val : listLong) {
        System.out.println(df.format(val / divider));
    }

Produces:

run:
1.5
8
BUILD SUCCESSFUL (total time: 0 seconds)

答案 3 :(得分:0)

This is because one of the two parameters are of type double.

You can simply achieve what you are trying to achieve by adding a cast to integer by doing: System.out.println((int) (distance2 / divider));

Also, you should use primitives as it requires only a fraction of the memory. Replace Long with long.

Here is the corrected code snippet:

public static void main (String[] args) throws Exception
{
    List<Long> listLong = new ArrayList<>();
    listLong.add(1500L);
    listLong.add(8000L);
    double divider = 1000;

    for (long val : listLong)
    {
        int calc = (int)(val/divider);
        if((val/divider - calc) == 0) {
            System.out.println(calc);
        } else {
            System.out.println(val/divider);
        }
    }
}

Output:

1.5
8

答案 4 :(得分:0)

use DecimalFormat

what works for me:

public class NewClass {

public static void main(String[] args) {
    DecimalFormat dcf= new DecimalFormat("#.#");
    Long distance = 1500L;
    Long distance2 = 8000L;
    double divider = 1000;

    System.out.println(dcf.format(distance / divider));
    System.out.println(dcf.format(distance2 / divider));
}

}

output

1.5
8