Implement AFMakeNumber

Also make stringToNumber be stubborn when converting strings to numbers

CCBUGS: 418445
This commit is contained in:
Albert Astals Cid 2020-03-08 17:09:51 +01:00
parent 9084bfaabe
commit d3b43b0b1e
2 changed files with 21 additions and 10 deletions

View file

@ -136,6 +136,16 @@ function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bC
return;
}
function AFMakeNumber(string)
{
var type = typeof string;
if ( type == "number" )
return string;
if ( type != "string" )
return 0;
return util.stringToNumber( string );
}
/** AFTime_Format
*
* Formats event.value based on parameters.

View file

@ -158,9 +158,10 @@ static KJSObject numberToString ( KJSContext *context, void *,
return KJSString( locale.toString( number, format.toLatin1(), precision ) );
}
/** Converts a String to a Number using l10n.
/** Converts a String to a Number trying with the current locale first and
* if that fails trying with the reverse locale for the decimal separator
*
* Number stringToNumber( String number, String LocaleName = system ) */
* Number stringToNumber( String number ) */
static KJSObject stringToNumber ( KJSContext *context, void *,
const KJSArguments &arguments )
{
@ -175,18 +176,18 @@ static KJSObject stringToNumber ( KJSContext *context, void *,
return KJSNumber( 0 );
}
QLocale locale;
if ( arguments.count() == 2 )
{
locale = QLocale( arguments.at( 1 ).toString( context ) );
}
const QLocale locale;
bool ok;
const double converted = locale.toDouble( number, &ok );
double converted = locale.toDouble( number, &ok );
if ( !ok )
{
return KJSNumber( std::nan( "" ) );
const QLocale locale2( locale.decimalPoint() == QLatin1Char('.') ? QStringLiteral("de") : QStringLiteral("en") );
converted = locale2.toDouble( number, &ok );
if ( !ok )
{
return KJSNumber( std::nan( "" ) );
}
}
return KJSNumber( converted );