1

I am trying to calculate an age in x++ where the customer is born on 1/6/2010 to the selected day of his visit - 1/6/2023 today but the result doesn't give me 13 years old but gives me 12.

 real ageDiffReal;
 int  ageDiffInt;
        
 date datetoday = DateTimeUtil::date(Visitas.RFC_DataVisita);
 ageDiffReal = (datetoday -  mkDate(dir.BirthDay,dir.BirthMonth,dir.BirthYear)) / 365.242199;
 ageDiffInt  = Round(ageDiffReal,0); 
     
 info(strFmt('%1,%2',ageDiffReal, ageDiffInt));

I tried with / 365 and with 365.25 because of leap years but still didn't work well

Info Result

R Monkey
  • 131
  • 1
  • 10
  • Take a look at the [DateTimeUtil::year](https://learn.microsoft.com/en-us/dotnet/api/dynamics.ax.application.datetimeutil.year#dynamics-ax-application-datetimeutil-year(microsoft-dynamics-ax-xpp-axshared-utcdatetime)) method. – FH-Inway Jan 06 '23 at 21:26

1 Answers1

6

You're using round(...) incorrectly.

  • ageDiffInt = decRound(ageDiffReal, 0); // Better
  • ageDiffInt = round(ageDiffReal, 1); // Works too

round(...) - The number that is a multiple of the value specified by the _decimals parameter and is closest to the value specified by the _arg parameter.

See https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-math-run-time-functions

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71