2

Dear all I am trying to round a Double number with three decimal places:

Double tl = (tReadTotal/totalRead).setScale(3);
c.CAC_Lectura__c = tl;

Doing this it does not work, How can I do that??

3
  • What about this doesn't work? Have you debugged the value of t1? What is the field type (and settings) for CAC_Lectura__c?
    – Derek F
    Commented Jan 29, 2020 at 12:42
  • Does not recognize the method setScale, so I cannot either debug it. The field type of CAC_Lectura__c is Number and Has a lenght of 15 and 3 decimal places. Commented Jan 29, 2020 at 12:44
  • Can you submit some value?And the expected one
    – user75427
    Commented Jan 29, 2020 at 12:49

1 Answer 1

6

As there is no method called setScale available for Double class, you are getting that exception. setScale method is available for Decimal class.

The scale (number of digits to the right of decimal point) can be set for a Decimal, but cannot for a Double. Doubles have limited number of methods available as compared to Decimals.

You can refer this Decimal or double? to understand when to use Decimal and when to use Double.

In your case, you will have to typecast the result of the division to Decimal and then set its scale.

Double tl = Decimal.valueOf((tReadTotal/totalRead)).setScale(3);
system.debug(tl);
c.CAC_Lectura__c = tl;

This above code typecasts the result to Decimal and then applies the scale which can be assigned to a Double

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .