Skip to content

Commit

Permalink
LDEV-5189 ParseNumber should support preciseMath
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Dec 9, 2024
1 parent 16f9252 commit a10b568
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
34 changes: 34 additions & 0 deletions test/functions/LSParseNumber.cfc
Original file line number Diff line number Diff line change
@@ -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
});

} );
}

}
59 changes: 59 additions & 0 deletions test/functions/ParseNumber.cfc
Original file line number Diff line number Diff line change
@@ -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 );
});

} );
}

}

0 comments on commit a10b568

Please sign in to comment.