如何将我的值转换为仅2位小数

时间:2017-03-17 19:25:06

标签: java decimal

到目前为止,我有这个,但我需要将我的值放在最多2位小数位,但由于某种原因,我现在正在绘制一个关于如何做到这一点的空白。

{
public static double celciusToFahrenheit(double celcius) 
{
  double fahrenheit = (9.0 / 5) * celcius + 32;
  return fahrenheit;
}

public static double fahrenheitToCelcius(double fahrenheit) 
{
  double celcius = (5.0 / 9) * (fahrenheit - 32);
  return celcius;
}   

  public static void main(String[] args) 
  { 
     System.out.println("Celcius\tFahrenheit\t|\tFahrenheit\tCelcius");

     for (int i = 0; i < 10; i++ ) 
     {

        double celcius = 40 - i;
        double convertedCelcius = celciusToFahrenheit(celcius);
        double fahrenheit = 120 - (i * 10);
        double convertedFahrenheit = fahrenheitToCelcius(fahrenheit);

        System.out.print(celcius + "\t" + convertedCelcius + "\t|\t" + fahrenheit + "\t" + convertedFahrenheit + "\n");
  }
   }
}

我的结果看起来像这样

Celcius Fahrenheit  |   Fahrenheit  Celcius
40.0    104.0   |   120.0   48.88888888888889
39.0    102.2   |   110.0   43.333333333333336
38.0    100.4   |   100.0   37.77777777777778
37.0    98.60000000000001   |   90.0    32.22222222222222
36.0    96.8    |   80.0    26.666666666666668
35.0    95.0    |   70.0    21.11111111111111
34.0    93.2    |   60.0    15.555555555555557
33.0    91.4    |   50.0    10.0
32.0    89.6    |   40.0    4.444444444444445
31.0    87.80000000000001   |   30.0    -1.1111111111111112

3 个答案:

答案 0 :(得分:1)

使用方法

<强> roundValue(convertedFahrenheit)

 public static double roundValue(double value, int numberOfDecimalPlaces) {
                 long factor = (long) Math.pow(10, numberOfDecimalPlaces);
            value = value * factor;
            long tmp = Math.round(value);
            return (double) tmp / factor;
        }

答案 1 :(得分:0)

试试这个

public static double round(double value, int places) 
{
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

答案 2 :(得分:0)

您可以使用两种辅助方法brace-style: "collapse,preserve-inline"padRight

roundTwoDecimalPlace

<强>输出:

import java.math.BigDecimal;
import java.math.RoundingMode;

class Main{
  public static double celciusToFahrenheit(double celcius) {
    double fahrenheit = (9.0 / 5) * celcius + 32;
    return fahrenheit;
  }

  public static double fahrenheitToCelcius(double fahrenheit) {
    double celcius = (5.0 / 9) * (fahrenheit - 32);
    return celcius;
  }

  public static double roundTwoDecimalPlace(double value) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd.doubleValue();
  }

  public static String padRight(String s, int n) {
    return String.format("%1$-" + n + "s", s);  
  }

  public static void main(String[] args) {
    System.out.println(padRight("Celcius", 15) + padRight("Fahrenheit", 15) + padRight("Celcius", 15) + padRight("Fahrenheit",15));
    for (int i = 0; i < 10; i++ ) {
      double celcius = roundTwoDecimalPlace(40 - i);
      double convertedCelcius = roundTwoDecimalPlace(celciusToFahrenheit(celcius));
      double fahrenheit = roundTwoDecimalPlace(120 - (i * 10));
      double convertedFahrenheit = roundTwoDecimalPlace(fahrenheitToCelcius(fahrenheit));
      System.out.println(padRight(String.valueOf(celcius), 15) + padRight(String.valueOf(convertedCelcius) , 15) + padRight(String.valueOf(fahrenheit), 15) + padRight(String.valueOf(convertedFahrenheit) ,15));
    }
  }
}

试试here!