1

I have a method which returns a double

public double getOdds() {
    return odds;
}

This works completly fine. However, the problem is when the data is displayed, it automatically rounds the inserted value up. I want to prevant that, and get a value with 2 decimals.

Here are my JSP where I call the method. The ArrayList "bets" consist of all values entered by the user. But, as explained above, when the user enters 2.75 then it will return 3.0

ArrayList <bets> bets = betsDAO.getBets(x, y);

for (bets bet : bets) {

    <td><%=bet.getOdds()%></td>
}

I'm still new to Java, and have tried looking for solutions, but unfortunatly I have not been able to solve the issue.

MEI
  • 25
  • 4
egx
  • 389
  • 2
  • 14
  • I don't have time to type out a full answer, but what you want is likely in the JSP JSTL `fmt:formatNumber` tag – Powerlord Feb 22 '20 at 16:45

3 Answers3

2

when the user enters 2.75 then it will return 3.0

The only reason for that to happen is if the value is limited to zero decimals at some point. Java only does that for int (or long or short or char or byte) values, and those are truncated, so result would be 2, not 3.0.

The only round-to-nearest I can envision is caused by the database or the JDBC driver:

  • The column type in the database is INTEGER or NUMBER(5,0) or something like that. Check your database schema.

  • The code reading the database (betsDAO) is calling getInt(...) and the JDBC driver rounds the value for you.

Anyway, the error is in code you haven't shown.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • thank you so much. Very helpfull answer. Both your "predictions" was correct. My columm in my database was set to INTEGER and the betsDAO used getInt(). I've changed both intances to Double and my code is now working. Thank you so much! – egx Feb 24 '20 at 13:02
  • @tobiasmadsen17 *FYI:* A "bet" sounds like a dollar amount, and you shouldn't use `double` for currency values, but use `BigDecimal` instead, and `NUMBER(15, 2)` in the database (replace `15` with size of your choice). --- [Why not use Double or Float to represent currency?](https://stackoverflow.com/q/3730019/5221149) – Andreas Feb 24 '20 at 15:18
1

it depends on which data type you intend on working with, but this

should do the trick for you

    public class Rounding {

        public static void main(String[] args) {
             //using string format
            double input = 3.14159265359;
            System.out.println("double : " + input);
            System.out.println("double : " + String.format("%.2f", input));

//using Decimal format
     double num = 1.34567;
        DecimalFormat df = new DecimalFormat("#.###");
        df.setRoundingMode(RoundingMode.CEILING);

        System.out.println(df.format(num));
        }

    }

OUTPUT:
double : 3.14159265359
double : 3.14
double : 3.14

OUTPUT:
1.346

so you can be more specific here with the decimal point to return. it all depends on the importance of the decimal precision for your project

Buchi
  • 368
  • 1
  • 4
  • 16
  • 1
    Well the problem is that the value if get is e.g. 3.0 because it automatically rounds up when the user enters a number. So when I enter 2.75 i'll get 3.0 output. – egx Feb 22 '20 at 16:28
  • 1
    @tobiasmadsen17 that happens because of the data type used for the value. if you use an int type for a value and a user inputs 2.7, it round up the value to the closest integer value, you should use double, to get the decimal values – Buchi Feb 22 '20 at 16:35
  • 1
    I have used this: Is there an alternative to "numbers"? – egx Feb 22 '20 at 16:37
  • 1
    you should use text in this situation
    to get the actual decimal number inputed and the parse the number to the decimal type at your backend function
    – Buchi Feb 22 '20 at 16:54
  • 1
    @Buchi *"if you use an int type for a value and a user inputs 2.7, it round up the value to the closest integer value"* --- Wrong. Casting a `double` to `int` would **truncate** the value to `2`, not round it up. – Andreas Feb 22 '20 at 18:20
0

Couldn't Math.floor help with the rounding? it rounds down, and if you ever need to use rounding up then use Math.ceil. hopefully this helps!

MEI
  • 25
  • 4
  • 1
    Unfortunatly not. I want to show 2 decimals. The decimals which the user enters. – egx Feb 22 '20 at 16:14