Money data type

Hello,

Could you help me to understand the purpose of the ‘MONEY’ data type ? What is the use case ?

Because in java when I get a Map<String, Object> and want to get value from MONEY column I have to cast the value object to Integer or Double (depending on whether the value contains decimals). So it’s not always the same casting which is not practical.

Thank you.

Hi. The MONEY column is just a DECIMAL type under the hood, but with fixed decimalScale = 2.
In java it corresponds to the BigDecimal type.
Working with the money calculation you should always prefer the BigDecimal or at least the double (8 byte) type, not the integer.

Hi @oleg-vyalyh

Thank you for the clarification on the MONEY type.

However, I notice that it is possible to store an integer in a column of type double or money. This is problematic because when retrieving a value from a double or money column in Java we expect to retrieve a value of the type of the column. Currently it is therefore possible that a double column returns an integer value which generates an exception when casting the value to the real type (double) of the column. Normally the value passed to a column of type double should be converted to a double to avoid this problem.

Thank you in advance for your answer.

Hi @Seb777

As far as I understand, to work with the Money column type
use BigDecimal.valueOf(value) when getting values and there will be no errors in casting regardless of whether ‘value’ is of Integer or Double type

Regards,
Viktor Mudrevsky

Hi @Viktor_Mudrevsky

Yes of course it’s possible to do as you suggest but if it’s the only way to do it, I guess it’s not possible to use the “Custom Class” mapping ? Because you would need to use a specific type for each class properties unless you use the generic “Object” type for all double or money type columns, which doesn’t seem very practical to me.

Regards,
Seb

@Seb777

you should use Double in java for Money type. Here it is an example:

And java code

public class TestMoney
{
  private Double money;

  public Double getMoney()
  {
    return money;
  }

  public TestMoney setMoney( Double money )
  {
    this.money = money;
    return this;
  }

  @Override
  public String toString()
  {
    return "money=" + money;
  }
}

And the main class:

public class App
{
  public static void main( String[] args ) throws InterruptedException
  {
    Backendless.initApp(...);

    List<TestMoney> list = Backendless.Data.of( TestMoney.class ).find();

    System.out.println( list );
  }
}

The result:

[money=5.55, money=5.0, money=5.5, money=5.35, money=null]

Hi @sergey.kuk

It’s working well !

Thank you.

Regards