Issues

Select view

Select search mode

 
27 of 27

Function round doesn't work correctly

Description

In the function round the value doesn't work aproperty; for example we have 0.175 decimal, and the round() with 2 decimals return 0.17 we spected 0.18
examples:

round(0.175,2) eq 0.18
round(40954.465,2) eq 40954.47
round(32768.465,2) eq 32768.47

this one does correctly
round(32767.465,2) eq 32768.47

I made a custom round function.

<cffunction name="RoundUp" returntype="Any" access="public">
<cfargument name="Numero" type="numeric" default="0.0" required="true"/>
<cfargument name="decimales" type="numeric" default="2" required="false"/>
<cfset var valores=toString(Numero)>
<cfset valores=ListToArray(valores,'.')><!--- Separe integer value off decimal value --->
<cfset var Signo= 1><!--- Default sign positive --->
<cfif valores[1] neq 0><!--- integer is zero --->
<cfset Signo= abs(valores[1])/valores[1]><!--- Sign of value --->
</cfif>
<cfset var valorRedondear=valores[2]><!--- get decimal value--->
<cfset valores[2]=left(valores[2],max(0,decimales))><!--- Separe the decimal values to the length required--->
<cfif mid(valorRedondear,decimales+1,1) gt 4><!--- if the next position value is greather than 4 --->
<cfset valores[2]+=1><!--- we acumulate to the decimal --->
</cfif>
<cfset valores[2]=Signo*('0.'&valores[2])><!--- convert the decimal, in decimal part --->
<cfreturn valores[1]+valores[2]><!--- return the float value result --->
</cffunction>

Environment

None

Details

Assignee

Reporter

Priority

Labels

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Affects versions

Created 24 November 2020 at 20:17
Updated 26 November 2020 at 14:39

Activity

Show:

Pothys - MitrahSoft26 November 2020 at 06:30

I've checked this ticket with 's PR in LDEV-2863. Yes, it works fine in Lucee and same as ACF.

KabutoTX25 November 2020 at 22:57

Yep. Both the OP’s and test work with my fix.

KabutoTX25 November 2020 at 22:35

Pothys - MitrahSoft25 November 2020 at 15:26

I've checked this ticket and confirmed the issue happened on lucee latest version 6.0.0.13-SNAPSHOT also. While using a round function with precision returns a different result for some values.

Flag notifications