function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ron WildRon Wild 

Converting number fields to Integer

I see the the Apex Math.round() function is being deprecated in favor of Math.roundToLong().

It looks like we now have to use the following code to convert a number field value from double to integer for use in functions that require integer parms (e.g. Date.newInstance(year,month,day))

Integer intValue = Integer.valueOf(String.valueOf(Math.roundToLong(object.Some_Number_Field__c)));

Are there plans to allow casting or conversion from Long to Integer in Apex?  Or at least, is there an easier way to achieve the same result than the example above?

Thanks,
cluebckecluebcke
Surprising that this thread has been dead for a year. Is there any solution to cast or convert a Long to an Integer? I can't believe that I actually have to convert it to a String first, but I can't see another way given the current API. This seems like a pretty fundamental operation, given that Apex methods generally don't seem to be overloaded to account for common numeric types.
Surgaya KhundrakpamSurgaya Khundrakpam
Considering   bjUnit.Square_Feet__c is a string objProperty.Square_Footage__c Accepts only whole number.
  objProperty.Square_Footage__c = '77.99';
  objProperty.Square_Footage__c = Integer.valueOf( Math.Round( Decimal.valueOf(objUnit.Square_Feet__c.replaceAll(',', '') ) ) );
System.debug( Integer.valueOf( Math.Round( Decimal.valueOf(objUnit.Square_Feet__c.replaceAll(',', '') ) ) ));

This will give us round figure of 78;
Surgaya KhundrakpamSurgaya Khundrakpam
1) Considering    Unit.Square_Feet__c is a string.   Unit.Square_Feet__c= '77.99';
2)  Property.Square_Footage__c accepts only whole number. 
   Property.Square_Footage__c = Integer.valueOf( Math.Round( Decimal.valueOf(Unit.Square_Feet__c.replaceAll(',', '') ) ) );
System.debug( Integer.valueOf( Math.Round( Decimal.valueOf(objUnit.Square_Feet__c.replaceAll(',', '') ) ) ));
Ch. Szandor KnappCh. Szandor Knapp
How about this:
 
Decimal dValue = 7.3;
Integer intConvertedValue = dValue.intValue();

Cf: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_decimal.htm
Surgaya KhundrakpamSurgaya Khundrakpam
Decimal d  = 143.5;
Long Ln = Math.roundToLong(d);
Integer intVersion = Integer.valueOf(Ln);
Wagner CoelhoWagner Coelho
Math.mod(Integer.valueOf(quantidade * 100000), Integer.valueOf(multiplo * 100000));