1

How can i round of to two decimal points with last decimal point can be either 0 , or 5

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String args[]) {
        String selling_price_beMod = "23.31";
        double sell_price_d = Double.parseDouble(selling_price_beMod);
        String selling_price = round(sell_price_d);
        System.out.println(selling_price);
    }

    public static String round(double value) {

        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(value);
    }

}

When i run the below program the output is

23.31

In this case i want to make it display as 23.35

Please excuse if this is a wrong question and should be approached in a different way .

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 2
    This is not the same, here the OP want to round to the upper decimal. He want the last decimal to be either 0 or 5. – Jean-François Savard Jan 13 '15 at 13:16
  • This is not question related to round of , but how to make it display as it looks like proper money . – Pawan Jan 13 '15 at 13:17
  • So in what cases you want it to be 0 or 5 at the end? So 0 if it's already a zero at the end and 5 at the end for any others? – ProgrammingIsAwsome Jan 13 '15 at 13:25
  • 1
    means if it ends with 1 , 2 ,3 , 4 , show it as 5 and if it ends with 6 , 7 , 8 , 9 end it as 0 – Pawan Jan 13 '15 at 13:28
  • You should really represent your currency as a long (down to the cent) or as a [`Currency`](http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html) object. That will avoid floating point arithmetic issues. Then, you are going to need to write a function to display the value. I think you can easily figure out what the last two digits would be, e.g., 09->10 or 01->05 (or whatever your requirements are), by taking the mod 100 of the value in cents. – Giovanni Botta Jan 13 '15 at 14:39

3 Answers3

2

I think that's what you want. Note that you should use DecimalFormat for a proper formatting, but anyway here an algorithm that would fix your issue :

public String roundNumber(double number)
{    
    int integer = Integer.parseInt(String.valueOf(number).split("\\.")[0]);
    String decimal = String.valueOf(number).split("\\.")[1];
    int decimalAsNumber = 0;

    if(!decimal.startsWith("0"))
        if(Integer.parseInt(decimal) < 10)
            decimalAsNumber = Integer.parseInt(decimal) * 10;
    else
        decimalAsNumber = Integer.parseInt(decimal);

    while(decimalAsNumber % 5 > 0)
        decimalAsNumber++;

    decimal = String.valueOf(decimalAsNumber);

    if(decimal.length() == 1)
        decimal += "0";

    return String.valueOf(integer) + "." + decimal;
}
  • It will always round to 0.*5 unless it ends with 0 or 5.
  • If there is no decimal, it will also display as .00
  • If it ends with anything %10 = 0, it will add the last 0 to make it proper money formatting.
  • anything higher than 5 will round up to the higher tenth.. Example .37 will round up to .40
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
0

First, you do not want to store your "money" as double value, but either as long or as Money instance (if you have Java 8, otherwise you could use a BigDecimal). Then your problem simply becomes writing the $ part, and then writing the rounded cent part. As you want to round upwards to 5 cents, you can easily use the following code:

long roundedCents = (((price % 100) / 20 + 4) * 20) // rounds upwards to the next …0 or …5 value

I'll leave the rest of the code as an exercise to the reader.

llogiq
  • 13,815
  • 8
  • 40
  • 72
0

This is my first post so I am sorry if this may not come helpful for you, but I tried something like this and it's working:

public class Test 
{
  public static void main(String args[]) 
  {
    String selling_price_beMod = "23.31";
    Double sell_price_d = Double.parseDouble(selling_price_beMod);
    Double selling_price = round(sell_price_d);
    System.out.println(selling_price);
  }

  public static Double round(Double value) 
  {
    int last = (value * 100) %10 >= 5 ? 5 : 0;
    int nr = (int) (value * 10);

    return nr/10.00 + last / 100.00;
  }
}
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
Darien
  • 1
  • 2
  • Please mind the [Java Naming Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) and you don't need to use the wrapper type `Double` all the time. Use the primitive type `double` if possible. – Tom Jan 13 '15 at 14:03
  • Ok, thank you for the observations! I am looking forward to improve my future posts. – Darien Jan 13 '15 at 14:11