1

I am trying to figure out how to show the correct number of digits after a decimal, based off a number passed into the method.

public static double chopDecimal(double value, int place)
{
    int chopped;
    //???
    return chopped;
}

So if the value passed is 123.456789 and the place is 2, it should show 123.45. The print statement is in another method.

 System.out.println("***MyMath ChopDecimal Test***");
    //Chop Decimal Test 1
    if (MyMath.chopDecimal(123.456789, 2) == 123.45)
    {
        System.out.println("Chop Decimal Test 1 Passed");
    }
    else
    {
        System.out.printf("Chop Decimal Test 1 Failed. Your answer: %f Correct Answer: 123.45\n",
                MyMath.chopDecimal(123.456789, 2));
    }
    //Chop Decimal Test 2
    if (MyMath.chopDecimal(.98765, 4) == .9876)
    {
        System.out.println("Chop Decimal Test 2 Passed");
    }
    else
    {
        System.out.printf("Chop Decimal Test 2 Failed. Your answer: %f Correct Answer: .9876\n",
                MyMath.chopDecimal(.98765, 4));
    }
Ryan
  • 11
  • 2

4 Answers4

1

This is possible using java.text.NumberFormat, although when you wish to convert to String for 'showing' purposes.

To match your example though, I've converted it babck to a double:

public static double chopDecimal(double value, int place)
{
    String chopped = NumberFormat.getInstance().setMaximumFractionDigits( place ).format( value );
    return Double.valueOf( chopped );
}
dev8080
  • 3,950
  • 1
  • 12
  • 18
1

I suggest you to use the java Rounding Mode. Simple example:

public static void main(String[] args) {
    System.out.println(chopDecimal(123.456789, 2));
}

public static String chopDecimal(double value, int place)
{
    // Parameter is the pattern
    DecimalFormat format = new DecimalFormat("0.00");
    format.setRoundingMode(RoundingMode.HALF_UP);

    return format.format(value);
}

This question is duplicated: How to round a number to n decimal places in Java

Reference: Oracle Documentation

Gabriel F
  • 161
  • 5
1

All of the answers above are pretty great, but if this for something you need to be able to explain, I wrote the segment of code below with all pretty basic tools of programming. Study this process below, it's not always about the solution, more so about can you come up with a solution. Always keep that in mind when solving any problem. There is always time later to improve your solution.

public static void main(String[] args) {
chopDecimal(123.456789,2);

}
public static double chopDecimal(double value, int place)
{
 String valToStr = Double.toString(value);
 int decimal=0;
 for(int i = 0; i < valToStr.length(); i++)
 {
  if(valToStr.charAt(i) == '.')
  {
   decimal = valToStr.indexOf(valToStr.charAt(i));
   System.out.println(decimal);
   break;
  }
 }
  
 String newNum = valToStr.substring(0,(++decimal+place));
 System.out.println(newNum);
 double chopped = Double.parseDouble(newNum);
  
 return chopped;
 }

}

Let me know if you have any questions.

0

You can use BigDecimal and setScale:

double chopped = BigDecimal
   .valueOf(value)
   .setScale(place, RoundingMode.DOWN)
   .doubleValue()
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76