how to leave the result 93.1

Asked

Viewed 98 times

-3

When rotating the following code:

package Aula_4;
import java.util.Scanner;
public class Exercício_3 {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        float n1,n2,n3,n4;

        System.out.println("Digite a largura da parede");
        n1 = entrada.nextFloat();
        System.out.println("Digite o comprimento da parede");
        n2 = entrada.nextFloat();
        System.out.println("Sua parede tem " +n1*n2+ "m²");
        System.out.println("Agora digite a largura do tijolo");
        n3 = entrada.nextFloat();
        System.out.println("Digite o Comprimento do tijolo");
        n4 = entrada.nextFloat();
        System.out.println("Para esta parede você irá precisar de aproximadamente " +(n1*n2*100) / (n3*n4*100));

The result shown is 93.13502, as round to 93,1?

  • 2

    I don’t understand why someone goes to class (in this case, Lesson 4 :) and doesn’t ask the teacher.

  • 3

    This answers your question? Double formatting in Java

2 answers

3

One solution is to use String format.:

Locale.setDefault(new Locale("pt", "BR"));
System.out.println(String.format("Para esta parede você irá precisar de aproximadamente :%.1f",(n1*n2*100) / (n3*n4*100)));

In this case, the '%. 1f' defines that it will present only one decimal place of its float variable, you can read more about the String.format in the official documentation. Note that to change the tab from "," to "." we have to define a new Locale with Locale.setDefault.

Another solution would be to use the Decimalformat:

float resultado=(n1*n2*100) / (n3*n4*100);
DecimalFormat format = new DecimalFormat("###,###.0");
System.out.println(format.format(resultado));

To use Decimalformat it is necessary to add:

import java.text.DecimalFormat;

It is worth remembering that, as explained by @hkotsubo Decimalformat is also affected by the locale, it is worth checking the example created by the: https://ideone.com/nwrwjX .

  • 1

    Still not going all, because the result that will be printed will be 93.1, and he’s asking 93,1

  • 2

    @Jeffersonquesado I think this depends on the default locale of the JVM: https://ideone.com/xhN1gq

  • 1

    @hkotsubo, that’s exactly what I was hoping for (too) in the answer, since the example provided by AP is using . for thousand separator (ie probably locale US).

  • 1

    @Jeffersonquesado Another alternative is to use Decimalformat, but actually I’m pretty sure the question is duplicated, I just didn’t find a good one to indicate...

  • As in the question he wrote "round", I thought the separator formatting was not relevant.

  • 1

    @Netocosta, it may be nitpicking for my part, but he complains that the result is 93.13502 but I wish I could 93,1. That is, unless it is a typo, it was intentional to complain of the decimal separator

  • I’ll edit the answer!

  • 1

    Just one detail, DecimalFormat is also affected by Locale default JVM, so ideally create one that uses the specific locale: https://ideone.com/nwrwjX

Show 3 more comments

2

Just an addendum. If the goal is just to print on the canvas, String.format is ideal, but if you want to store the number in a variable, use

Math.round((n1*n2*100) / (n3*n4*100)*10)/10;

Remembering that it is necessary to import the java.util.Math package

Browser other questions tagged

You are not signed in. Login or sign up in order to post.