Code that leverages decimal value in C # for Java / Android

1

I have a code that rounds a value of type decimal to homes I want, and I would like to implement it in Android in Java but I'm having some difficulties, here's the code below in C #.

public static class Valor
{
    public static decimal Arredondar(decimal valor, int casasDecimais)
    {
        var valorNovo = decimal.Round(valor, casasDecimais);
        var valorNovoStr = valorNovo.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
        return decimal.Parse(valorNovoStr);
    }

    public static decimal? Arredondar(decimal? valor, int casasDecimais)
    {
        if (valor == null) return null;
        return Arredondar(valor.Value, casasDecimais);
    }
}

As I researched I would have to use Bigdecimal but I'm having several problems.

    
asked by anonymous 16.12.2015 / 18:37

1 answer

1

I'm not sure what the goal is, but it does not seem to need BigDecimal (except for something on the Java side, but C # is C #). Decimal has no rounding problems. It actually seems to me to be a lot simpler to round off than it is in this code. Unless you have some objective that is not in the question:

public static decimal Arredondar(decimal valor, int casasDecimais) {
    return decimal.Round(valor, casasDecimais);
}

See working on dotNetFiddle .

It's so simple that I do not even need this method.

I do not see why it would make a difference in Android. Do not mix the dice with the dice presentation.

For the nullable, you have a way to use the same method as long as you use the null propagation of C # 6 . For non-nullable, you do not need to use this method, but if you want to use it to maintain consistency, okay. For this I used the extension method.

See running on dotNetFiddle .

    
16.12.2015 / 18:52