CultureInfo.validateNumber

Syntax

validateNumber(ValObj: Object, GroupSeparator: String, DecimalSeparator: String, FractionCount: Number);

Parameters

ValObj. JSON object that has the value field containing the source string.

GroupSeparator. Group separator. If it is not specified, the parameter value is obtained from the regional settings.

DecimalSeparator. Decimal separator. If it is not specified, the parameter value is obtained from the regional settings.

FractionCount. Number of digits in decimal part. If this parameter is not specified the number of digits is not limited.

Description

The validateNumber method converts specified string to a number, if it is possible.

Example

To execute the example, add a link to PP.js scenario file to HTML page. Determine if the strings 0,2, 0.2, 100`000 and 100 000 are numbers and format the number 500000.12 in accrodance with thousand and decimal separators specified in parameters of the numberToFormattedString method:

var cultureInfo = new PP.CultureInfo();
// Set decimal separator
cultureInfo.DecimalSeparator = ",";
console.log("Decimal separator: «" + cultureInfo.DecimalSeparator + "»");
//Check if 0.2 and 0,2 values are numbers for "Russian culture"
var isNumber = cultureInfo.validateNumber({
    value: "0.2"
});
console.log("Value «0.2» " + (isNumber ? "is" : "is not") + " a number. ");
isNumber = cultureInfo.validateNumber({
    value: "0,2"
});
console.log("Value «0,2» " + (isNumber ? "is" : "is not") + " a number.");
// Set group separator
cultureInfo.GroupSeparator = "`";
console.log("Group separator: «" + cultureInfo.GroupSeparator + "»");
isNumber = cultureInfo.validateNumber({
    value: "100`000"
});
console.log("Value «100`000» " + (isNumber ? "is" : "is not") + " a number.");
isNumber = cultureInfo.validateNumber({
    value: "«100 000» "
});
console.log("Value «100 000» " + (isNumber ? "is" : "is not") + " a number.");
// Format the number considering separators
console.log("Formatted number: " + cultureInfo.numberToFormattedString(500000.123, " ", ".", 2));

After executing the example the browser console displays notices if the strings 0,2, 0.2, 100`000 and 100 000 are numbers. Before that decimal and thousand separators different from values in regional settings, are defined in the DecimalSeparator and GroupSeparator properties:

Decimal separator: ,
The value 0.2 is not a number.
The value 0,2 is a number.
Thousand separator: `
The value 100`000 is a number.
The value 100 000 is not a number.
 

The browser console displays formatted number 500000.12 accounting for decimal and thousand separators specified as parameters of the numberToFormattedString method:

Formatted number: 500 000.12

See also:

CultureInfo