A Percise Parser
by in CodeSOD on 2026-02-03Thomas worked for a company based in Germany which was looking to expand internationally. Once they started servicing other locales, things started to break. It didn't take long to track the problem down to a very "percise" numeric parser.
handleInput( value ){
let value_ = value;
if( value.substring( 0, 1 ) === '+' ){
value_ = value.substring( 1 );
}
value_ = value_.split( '.' ).join( '' );
if( this.usePercisionIfPercentage && value_.indexOf( ',' ) >= 0 ) {
const parsedPreValue = value_.split( ',' )[ 0 ];
const parsedCommaValue = parseInt( value_.split( ',' )[ 1 ], 10 ) < 10 ?
parseInt( value_.split( ',' )[ 1 ], 10 ) * 10 : value_.split( ',' )[ 1 ].substring( 0, 2 );
if( parsedCommaValue === 0 ) {
value_ = parseInt( parsedPreValue, 10 );
}
else {
const parsedValue = parseInt( parsedPreValue + parsedCommaValue, 10 );
value_ = parseInt( parsedValue, 10 ) / 100;
}
}
// do stuff with value_
}