Decimal support?


#1

Is there any plan to support ‘decimal’? (or BigDecimal in java)
As you know it is an important variable type used in accounting systems, and using long or byte array is not a good idea!


#2

Yes - we have an open issue in our backlog, but that hasn’t been prioritized as we haven’t received many requests for supporting it.


#3

Really? I think most people already stop considering realm in the first place, “because” there is no support for decimal!


#4

Certainly is important for us! If it’s of any help to anyone we implement decimals like this

[MapTo(nameof(ItemCost))]
private double _ItemCost { get; set; }
public decimal ItemCost
{
    get => (decimal)Math.Round(_ItemCost, 2);
    set => _ItemCost = Math.Round((double)value, 2);
}

Please let me know if anyone thinks this is dumb :slight_smile:


#5

Decimal gets my vote too.

BTW, since you are prioritizing things also according to people requests, maybe it would be nice to create some kind of poll, so people can vote?


#6

You can upvote on the GitHub link


#7

It’s not ideal, but one way you can work around it for now is like this:

class MyObject : RealmObject
{
    public decimal DecimalValue
    {
        get
        {
            var bits = new[]
            {
                Decimal_0,
                Decimal_1,
                Decimal_2,
                Decimal_3,
            };

            return new decimal(bits);
        }
        set
        {
            var bits = decimal.GetBits(value);
            Decimal_0 = bits[0];
            Decimal_1 = bits[1];
            Decimal_2 = bits[2];
            Decimal_3 = bits[3];

            DoubleValue = decimal.ToDouble(value);
        }
    }

    public double DoubleValue { get; private set; }

    private int Decimal_0 { get; set; }
    private int Decimal_1 { get; set; }
    private int Decimal_2 { get; set; }
    private int Decimal_3 { get; set; }
}

This way you store the decimal precisely in the 4 integer components, yet you have its double approximation, which should help for querying (because querying by the int components would be quite cumbersome).