From a10b568e697438e2d89a804356621b335c561bcb Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Mon, 9 Dec 2024 15:36:45 +0100 Subject: [PATCH] LDEV-5189 ParseNumber should support preciseMath https://luceeserver.atlassian.net/browse/LDEV-5189 --- .../runtime/functions/string/ParseNumber.java | 2 +- test/functions/LSParseNumber.cfc | 34 +++++++++++ test/functions/ParseNumber.cfc | 59 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/functions/LSParseNumber.cfc create mode 100644 test/functions/ParseNumber.cfc diff --git a/core/src/main/java/lucee/runtime/functions/string/ParseNumber.java b/core/src/main/java/lucee/runtime/functions/string/ParseNumber.java index 84237a6d42..1434d0045c 100644 --- a/core/src/main/java/lucee/runtime/functions/string/ParseNumber.java +++ b/core/src/main/java/lucee/runtime/functions/string/ParseNumber.java @@ -85,7 +85,7 @@ else if (strRadix.startsWith("hex")) { if (strNumber.indexOf('.') != -1 && radix != DEC) throw new ExpressionException("The radix con only be [dec] for floating point numbers"); if (radix == DEC) { - return Caster.toDoubleValue(strNumber); + return Caster.toNumber(strNumber); // this should return a bigInteger when preciseMath is enabled } return Integer.parseInt(strNumber, radix); } diff --git a/test/functions/LSParseNumber.cfc b/test/functions/LSParseNumber.cfc new file mode 100644 index 0000000000..6e10727b67 --- /dev/null +++ b/test/functions/LSParseNumber.cfc @@ -0,0 +1,34 @@ +component extends="org.lucee.cfml.test.LuceeTestCase" { + function beforeAll(){ + variables.preciseMath = getApplicationSettings().preciseMath; + }; + + function afterAll(){ + application action="update" precisemath=variables.preciseMath; + }; + + function run( testResults, testBox ){ + describe( "Test LSParseNumber", function(){ + + it( "Test with Large number", function(){ + var n = "2.305.843.009,01"; + var locale = "german (standard)"; + application action="update" preciseMath=false; + expect( LSParseNumber( n, locale ) ).toBe( 2305843009 ); + application action="update" preciseMath=true; + expect( LSparseNumber( n, locale ) ).toBe( 2305843009 ); // but it returns 2305843009.01 ? + }); + + it( "Test with Large number", function(){ + var n = "2.305.843.009.213.693.951,01"; + var locale = "german (standard)"; + application action="update" preciseMath=false; + expect( LSParseNumber( n, locale ) ).toBe( 2305843009213693696 ); + application action="update" preciseMath=true; + expect( LSparseNumber( n, locale ) ).toBe( 2305843009213693952 ); // LSParseNumber doesn't support big numbers + }); + + } ); + } + +} diff --git a/test/functions/ParseNumber.cfc b/test/functions/ParseNumber.cfc new file mode 100644 index 0000000000..2d476a5c8e --- /dev/null +++ b/test/functions/ParseNumber.cfc @@ -0,0 +1,59 @@ +component extends="org.lucee.cfml.test.LuceeTestCase" { + function beforeAll(){ + variables.preciseMath = getApplicationSettings().preciseMath; + }; + + function afterAll(){ + application action="update" precisemath=variables.preciseMath; + }; + + function run( testResults, testBox ){ + describe( "Test ParseNumber", function(){ + + it( "Test with smaller number", function(){ + var n = "2305843.44"; + application action="update" preciseMath=false; + expect( parseNumber( n ) ).toBe( 2305843.44 ); + application action="update" preciseMath=true; + expect( parseNumber( n ) ).toBe( 2305843.44 ); + }); + + it( "Test with Large number", function(){ + var n = "2305843009213693951.77"; + application action="update" preciseMath=false; + expect( parseNumber( n ) ).toBe( 2305843009213693696 ); + application action="update" preciseMath=true; + expect( parseNumber( n ) ).toBe( 2305843009213693951 ); // returns 2305843009213693951.77 + }); + + it( "Test Bin", function(){ + application action="update" preciseMath=false; + var n = "1000"; + var radix = "bin"; + expect( parseNumber( n, radix ) ).toBe( 8 ); + application action="update" preciseMath=true; + expect( parseNumber( n, radix ) ).toBe( 8 ); + }); + + it( "Test Oct", function(){ + application action="update" preciseMath=false; + var n = "1000"; + var radix = "oct"; + expect( parseNumber( n, radix ) ).toBe( 512 ); + application action="update" preciseMath=true; + expect( parseNumber( n, radix ) ).toBe( 512 ); + }); + + it( "Test Hex", function(){ + application action="update" preciseMath=false; + var n = "1000"; + var radix = "hex"; + expect( parseNumber( n, radix ) ).toBe( 4096 ); + application action="update" preciseMath=true; + expect( parseNumber( n, radix ) ).toBe( 4096 ); + }); + + } ); + } + +}