-1

This might be a strange question, but how do i truncatean array of floats to e.g.: 0.2f (2 decimal places).

Imagine i have a float array X that contains 10 elements with the value 2.123456. I want to make them 2.123.

Can anybody help?

Kind regards

Pedro Neves
  • 364
  • 1
  • 8
  • 26
  • 1
    Take a look at `Math.round(float)`. – Sotirios Delimanolis Sep 26 '13 at 22:29
  • Take a look here: http://www.mkyong.com/java/how-to-round-double-float-value-to-2-decimal-points-in-java/ – Anderson Matos Sep 26 '13 at 22:32
  • 1
    If you need to control decimal places in Java, consider using BigDecimal rather than float. Many values such as 2.123 can be exactly represented in BigDecimal, but not in float or double. – Patricia Shanahan Sep 26 '13 at 22:42
  • Ok thanks by the tip of BigDecimal. – Pedro Neves Sep 26 '13 at 22:47
  • Your example sounds like it only has one float? Is this a float array with size of 1???? Like everyone else said, your problem really amounts to how do you round floats in general - irregardless of its multiple floats or just one. – user919860 Sep 26 '13 at 23:05
  • I basically want to truncate (maybe round wasn't the best word) the float values of a float array. Imagine the array stores the following values: 0.123456, 0.654321, 0.789456, 0.564789 i want to get 0.1234, 0.6543, 0.7894, 0.5647 – Pedro Neves Sep 26 '13 at 23:14

1 Answers1

2

Look at the following code:

public class Truncate {
  public static void main(String[] args) {
    System.out.println(truncate(2.123556f));
  }

  public static float truncate(float f) {
    // after casting it to int, it will remove all decimal values.
    // float bigInt = (int) (Math.round(f * 1000)); // prints 2.124 for f=2.123556f
    float bigInt = (int) (f * 1000); // prints 2.123 for f=2.123556f
    float result = bigInt / 1000;

    return result;
  }
}

You can create your own truncate method or use a round if needed. That way you will force it to be 3 decimals. If you need it dynamically then you will need to play with the "1000"

It does not matter if it is an array or a float, just loop the array to call your truncate method.

UPDATE:

Maybe not the most elegant but you can use big decimals like this...

import java.math.BigDecimal;

public class Truncate {
  public static void main(String[] args) {
    System.out.println(truncate(new BigDecimal(0.13642351627349847f)));
  }
  public static BigDecimal truncate(BigDecimal number) {
    BigDecimal thousand = new BigDecimal(1000);
    BigDecimal mult = new BigDecimal(number.multiply(thousand).intValue()); 
    BigDecimal result = mult.divide(thousand);
    return result;
  }
}
porfiriopartida
  • 1,546
  • 9
  • 17
  • 1
    With your code, for the value 0.13642351627349847 i got 0.13599996566772454. What i wanted is 0.1364. Can u give me a tip? – Pedro Neves Sep 26 '13 at 23:07
  • 1
    @PedroNeves I got `0.136` with the code as is. This was my call: `truncate(0.13642351627349847f)` BUT that's why you usually use BigDecimal if you have this kind of accuracy issues. – porfiriopartida Sep 26 '13 at 23:12