Wątek przeniesiony 2017-01-17 11:49 z Java przez ŁF.

java Zaokrąglenie do 2 miejsc po przecinku

0

Mam problem ponieważ String.format("%.2f", z) zmienia mi liczbe na String i w dalszej częsci program nie liczy poniewaz krzyczy Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "28,6" gsyż String.format("%.2f", z) zmienia mi na String czyli liczbę z przecinkiem i czy wie ktos jak zaokrąglić liczby w moim przypadku zeby pozostały z kropkami w polach tekstowych ?

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
double x, y, z, a, b, c, d, e;
double x1, y1, a1, b1, c1, d1, e1;
double x2, y2, a2, b2, c2, d2, e2;
double x3, y3, a3, b3, c3, d3, e3;

    if (V1.getText().trim().isEmpty()) {
        x = 0;
    } else {
        x = Double.parseDouble(V1.getText());
    }
    if (V2.getText().trim().isEmpty()) {
        y = 0;
    } else {
        y = Double.parseDouble(V2.getText());
    }
    if (V3.getText().trim().isEmpty()) {
        a = 0;
    } else {
        a = Double.parseDouble(V3.getText());
    }
    if (V4.getText().trim().isEmpty()) {
        b = 0;
    } else {
        b = Double.parseDouble(V4.getText());
    }
    if (V5.getText().trim().isEmpty()) {
        c = 0;
    } else {
        c = Double.parseDouble(V5.getText());
    }
    if (V6.getText().trim().isEmpty()) {
        d = 0;
    } else {
        d = Double.parseDouble(V6.getText());
    }
    if (V7.getText().trim().isEmpty()) {
        e = 0;
    } else {
        e = Double.parseDouble(V7.getText());
    }
    if (I1.getText().trim().isEmpty()) {
        x1 = 0;
    } else {
        x1 = Double.parseDouble(I1.getText());
    }
    if (I2.getText().trim().isEmpty()) {
        y1 = 0;
    } else {
        y1 = Double.parseDouble(I2.getText());
    }
    if (I3.getText().trim().isEmpty()) {
        a1 = 0;
    } else {
        a1 = Double.parseDouble(I3.getText());
    }
    if (I4.getText().trim().isEmpty()) {
        b1 = 0;
    } else {
        b1 = Double.parseDouble(I4.getText());
    }
    if (I5.getText().trim().isEmpty()) {
        c1 = 0;
    } else {
        c1 = Double.parseDouble(I5.getText());
    }
    if (I6.getText().trim().isEmpty()) {
        d1 = 0;
    } else {
        d1 = Double.parseDouble(I6.getText());
    }
    if (I7.getText().trim().isEmpty()) {
        e1 = 0;
    } else {
        e1 = Double.parseDouble(I7.getText());
    }
    if (C1.getText().trim().isEmpty()) {
        x2 = 0;
    } else {
        x2 = Double.parseDouble(C1.getText());
    }
    if (C2.getText().trim().isEmpty()) {
        y2 = 0;
    } else {
        y2 = Double.parseDouble(C2.getText());
    }
    if (C3.getText().trim().isEmpty()) {
        a2 = 0;
    } else {
        a2 = Double.parseDouble(C3.getText());
    }
    if (C4.getText().trim().isEmpty()) {
        b2 = 0;
    } else {
        b2 = Double.parseDouble(C4.getText());
    }
    if (C5.getText().trim().isEmpty()) {
        c2 = 0;
    } else {
        c2 = Double.parseDouble(C5.getText());
    }
    if (C6.getText().trim().isEmpty()) {
        d2 = 0;
    } else {
        d2 = Double.parseDouble(C6.getText());
    }
    if (C7.getText().trim().isEmpty()) {
        e2 = 0;
    } else {
        e2 = Double.parseDouble(C7.getText());
    }
    if (VV1.getText().trim().isEmpty()) {
        x3 = 0;
    } else {
        x3 = Double.parseDouble(VV1.getText());
    }
    if (VV2.getText().trim().isEmpty()) {
        y3 = 0;
    } else {
        y3 = Double.parseDouble(VV2.getText());
    }
    if (VV3.getText().trim().isEmpty()) {
        a3 = 0;
    } else {
        a3 = Double.parseDouble(VV3.getText());
    }
    if (VV4.getText().trim().isEmpty()) {
        b3 = 0;
    } else {
        b3 = Double.parseDouble(VV4.getText());
    }
    if (VV5.getText().trim().isEmpty()) {
        c3 = 0;
    } else {
        c3 = Double.parseDouble(VV5.getText());
    }
    if (VV6.getText().trim().isEmpty()) {
        d3 = 0;
    } else {
        d3 = Double.parseDouble(VV6.getText());
    }
    if (VV7.getText().trim().isEmpty()) {
        e3 = 0;
    } else {
        e3 = Double.parseDouble(VV7.getText());
    }
    
    



    z = x1 * x2 * 23 / 100;
    VV1.setText("" + z + "");
    double i = Double.parseDouble(VV1.getText());
    z = y1 * y2 * 23 / 100;
    VV2.setText("" + z + "");
    z = a1 * a2 * 23 / 100;
    VV3.setText("" + z + "");
    z = b1 * b2 * 23 / 100;
    VV4.setText("" + z + "");
    z = c1 * c2 * 23 / 100;
    VV5.setText("" + z + "");
    z = d1 * d2 * 23 / 100;
    VV6.setText("" + z + "");
    z = e1 * e2 * 23 / 100;
    VV7.setText("" + z + "");
    z = x1 * x2 + x3;
    V1.setText("" + z + "");
    double j = Double.parseDouble(V1.getText());
    z = y1 * y2 + y3;
    V2.setText("" + z + "");
    z = a1 * a2 + a3;
    V3.setText("" + z + "");
    z = b1 * b2 + b3;
    V4.setText("" + z + "");
    z = c1 * c2 + c3;
    V5.setText("" + z + "");
    z = d1 * d2 + d3;
    V6.setText("" + z + "");
    z = e1 * e2 + e3;
    V7.setText("" + z + "");
    z = x + y + a + b + c + d + e;
    TFVSuma.setText(""+String.format("%.2f", z)+"");
    TFVSuma1.setText(""+String.format("%.2f", z)+"");
    z = (x1 * x2) + (y1 * y2) + (a1 * a2) + (b1 * b2) + (c1 * c2) + (d1 * d1) + (e1 * e2);
    TFSuma.setText(""+String.format("%.2f", z)+"");
1

Po pierwsze jeśli chcesz liczyć z dokładnością do 2 miejsc po przecinku to nie robi się tego konwertując na Stringa i znowu parsując (a to zgaduję robisz w dalszej części).

Do tego używa się typu BigDecimal (i setScale) (zamiast double).

Po drugie - program jest straszny - zwrócił(e|a)ś uwagę, że ten kod, który napisałeś ma dość dużo podobnych linii ?

Po trzecie String.format(Locale.ENGLISH, "%.2f", z) ; // pomoże - ale w świetle tego co wyżej, będzie to kod - horror klasy B

0

no niestety masz racje zajmuje sie tym od miesiaca moze dlatego chociaz to nie jest usprawiedliwienie a masz pomysl na skrócenie if-ów ? bo zadna petla raczej nie zrobie tego gdyz do kazdego jPolaTextowego musi byc oddzielnie spełniony warunek .

0

Mógłbyś pomóc mi w tym ?

0

Wystarczy napisać metodę

private BigDecimal checkString(String text){
if (text.trim().isEmpty()) {
       return BigDecimal.ZERO;}
return new BigDecimal(text);
  
0

@zebra, spróbuj zrobić dwie HashMapy typu HashMap<JTextField,Double>. Jedna na dane wejściowe, druga na wyniki.
@kompleks, w kodzie jest usterka, metoda parseDouble dopuszcza spacje na początku i na końcu (ignoruje je), a konstruktor new BigDecimal(String) nie dopuszcza.

return new BigDecimal(text.trim());
0

Dzięki za pomoc !

1 użytkowników online, w tym zalogowanych: 0, gości: 1