Java Float Number Round round(float amount, int cent)

Here you can find the source of round(float amount, int cent)

Description

round

License

Open Source License

Declaration

public static float round(float amount, int cent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static float round(float amount, int cent) {
        if (cent <= 0) {
            throw new IllegalArgumentException("cent must be greater than 0");
        }/* w ww. j  a v a 2s . co  m*/

        int roundAmount = Math.round(amount * 100.0f);
        int x = roundAmount % cent;
        int discount = 0;
        if (x > cent / 2.0f) {
            discount = cent - x;
        } else {
            discount = -x;
        }
        return (roundAmount + discount) / 100.0f;
    }

    public static float round(float amount) {
        return Math.round(amount * 100.0f) / 100.0f;
    }
}

Related

  1. round(final float f)
  2. round(final float val)
  3. round(final float value)
  4. round(final float x, final float roundFactor)
  5. round(float a)
  6. round(float axisValue)
  7. round(float d)
  8. round(float f, int dp)
  9. round(float f, int i)