在 X++ 中计算年龄

R M*_*key 1 x++ dynamics-365-operations dynamics-365

我正在尝试计算 x++ 中的年龄,其中客户出生于 2010 年 1 月 6 日到他访问的选定日期 - 2023 年 1 月 6 日,但结果没有给我 13 岁,而是给我 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));
Run Code Online (Sandbox Code Playgroud)

由于闰年,我尝试使用 / 365 和 365.25 但仍然效果不佳

信息结果

Ale*_*tny 6

你使用round(...)方法不对。

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

round(...)- _decimals 参数指定的值的倍数且最接近 _arg 参数指定的值的数字。

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