Validate Integer

This is the scenario, on Lucee 5.4.6.9:

var startingFloat = 76.0426;

var twoFloatNumber = DecimalFormat( startingFloat );
dump( twoFloatNumber );
// write: 76.04

var thisInteger = twoFloatNumber*100;
dump( thisInteger );
// write: 7604

dump( isValid( 'integer', thisInteger ) );
// FALSE (!)

I don’t understand because 7604 is not Integer.
I have to use Val() or Int() function around moltiplication to obtain TRUE.

var thisInteger = Val( twoFloatNumber*100 );

Any hint?

I think the issue may be that DecimalFormat() doesn’t actually change the startingFloat value, it simply returns a string formatted with 2 decimal places.

The underlying value is still 76.0426, not 76.04, so multiplying it by 100 won’t give you an integer.

Actually, I don’t think that is right, sorry. It seems to be something about that particular value as I’ve tried others where the result does validate as an integer. Puzzling…

1 Like

It seems to be the multiplication step where the problem arises. Nothing to do with DecimalFormat(). A simpler repro case:

WriteDump( IsValid( "integer", ( 76.04 * 100 ) ) )
//expected: true, Lucee 5.4.6.9 returns false

But use a different decimal value such as 56.04 and the validation passes.

Again, my hunch is that the multiplication operation is not actually returning an integer in certain cases for some reason, even though the dumped value appears to be one.

ACF 2023 seems to behave in the same way as Lucee 5.4.6.9 (i.e. incorrectly), but Lucee 6.1.1.118 correctly validates ( 76.04 * 100 ) as an integer

It looks like both Lucee 5 and ACF return the multiplication result as a java Double whereas Lucee 6 returns a BigDecimal.

I’m no java expert but I it seems that Doubles are bit flaky when it comes to floating point operations, which is perhaps why Lucee 6 has moved to BigDecimal.

2 Likes

@Roberto_Marzialetti , I tested this in the lucee latest version (6.2.0.205-SNAPSHOT), and it worked as expected.

2 Likes

Yes, in 6.* version is Ok.
The problem is on 5.* versions.