# How to limit the number of decimals of a double?

10

As the title says, I want to limit the number of decimals that results from an operation with double type variables, this result is shown in a JTextArea of a JFrame.

The code that does the operation and prints the result in the JTextArea is the following: ( `la` , `lb` and `lc` represent the sides of a triangle, `a` calculate the area and `A` is the JTextArea where the result is printed).

``````if (lb+lc>la && la+lc>lb && la+lb>lc){
a=Math.sqrt((la+lb+lc)*(-la+lb+lc)*(la-lb+lc)*(la+lb-lc)/16);
p=la+lb+lc;
A.setText("El triangulo es "+t+".\nEl area es ("+a+").\nEl perimetro es ("+p+").");
``````

asked by Roy 23.10.2016 в 18:20
source

13

There are many ways to limit the number of decimals of output (Presentation) of a `double` , but not the number itself, for example for `double number = 1.4159999999;` limit the output to 2 decimals. if you want more digits it is a matter of adding zeros in `decimalformat` and `Round, cambiar el 2 por por la cantidad deseada en` String.format `y` printf `Y y en` setScale `de` BigDecimal '

• Using DecimalFormat

`````` DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(number));
/* Salida : 1.42*/
``````
• Using String.Format

``````System.out.println(String.format("%.2f", number));
/* Salida : 1.42*/
``````
• If you only want the output to have that format, you would apply numberformat

``````System.out.printf("Valor: %.2f", number );
/* Salida : 1.42*/
``````
• Using Math.Round () where the number of zeros is the number of decimal places to limit

``````System.out.println((double)Math.round(number * 100d) / 100d);
/* Salida : 1.42*/
``````
• Using the class BigDecimal , using the setScale that receives two parameters the number of decimals limit and rounding mode

``````BigDecimal bd = new BigDecimal(number);
bd = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println(bd.doubleValue());
/* Salida 1.42*/
``````
• answered by 23.10.2016 / 18:35
source
6

Test using String.format

``````double valor = 123.456789;

String.format("%.3f", valor)  // 123.456
``````

You can combine everything like that, for this case it is a compact form and it allows you to format everything together in a single line.

``````String.format("El triangulo es %s.\nEl area es (%.3f).\nEl perimetro es (%.3f).", t, a, p)
``````

Note that format automatically takes the locale configuration, so depending on the country, you can use `.` or `,` as a decimal point separator.

4

You can use a `DecimalFormat` . For example:

``````DecimalFormat formato = new DecimalFormat("#.000");

System.out.println(formato.format(p));
``````

It will return your value to three decimals. You would have to add (or remove) as many zeros as you want decimals after the whole number.

2

The best thing I've found to reduce the number of decimals of a double:

number: the value with all the decimals

NumberDecimals: how many decimals do I want to recover?

Ahem: 2.8049999999999997 returns 2,805

double val = formattingDecimals (2.8049999999999997, 3);

``````public static Double formatearDecimales(Double numero, Integer numeroDecimales) {
return Math.round(numero * Math.pow(10, numeroDecimales)) / Math.pow(10, numeroDecimales);
}
``````

``````Double answer = 234.32545;