-2

I have this so far but I'm needing to get my values at the most to 2 decimal places but for some reason I'm drawing a blank on how to do it right now.

{
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");
  }
   }
}

and my result looks like this

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
derekg8881
  • 97
  • 7

3 Answers3

1

Use method

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;
        }
Bhushan Uniyal
  • 4,839
  • 2
  • 16
  • 36
  • As long as you're using magic numbers in `(long) Math.pow(10, 2)` anyway, what's wrong with just using `100L`? – Lew Bloch Mar 17 '17 at 19:46
  • @LewBloch here i have not used 100L direct because may be user want roundup till three, so here he just change the value of 2 and use 3. I know i have hard coded the value, which is wrong because we should use run time parameter here which will return the value as per requested parameter but here he just mention only 2 as hardcoded. – Bhushan Uniyal Mar 20 '17 at 09:35
  • I'm sorry, what? Rounding has nothing to do with the fact that `10` squared is `100`, no rounding required. But I really can't parse your point out of what you said. – Lew Bloch Mar 20 '17 at 16:40
  • @LewBloch I mean suppose user want value with 2 decimal places e.g : Input = 12.569 -> So in this case output should be 12.56 Now the user requirement change and user need value with 3 decimal places e.g: input = 12.5869 then output should be 12.586. We can achieve this requirement with above code, I have update the code, please have a look. also we need round for the closest long or int. – Bhushan Uniyal Mar 20 '17 at 18:07
  • Wouldn't `12.569` round to `12.57`? – Lew Bloch Mar 20 '17 at 19:02
  • @LewBloch yes it will. – Bhushan Uniyal Mar 20 '17 at 19:11
0

try this

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();
}
Eclipse
  • 175
  • 1
  • 11
0

You could use two auxiliary methods padRight and 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));
    }
  }
}

Output:

Celcius        Fahrenheit     Celcius        Fahrenheit     
40.0           104.0          120.0          48.89          
39.0           102.2          110.0          43.33          
38.0           100.4          100.0          37.78          
37.0           98.6           90.0           32.22          
36.0           96.8           80.0           26.67          
35.0           95.0           70.0           21.11          
34.0           93.2           60.0           15.56          
33.0           91.4           50.0           10.0           
32.0           89.6           40.0           4.44           
31.0           87.8           30.0           -1.11

Try it here!

Sash Sinha
  • 11,515
  • 3
  • 18
  • 35