1

The negative numeric value I'm getting from the back-end application is formatted according to our internal configuration (UI user profile). In other words, for value -23.79879 my xml input may be <myNumber>-23.79879</myNumber> or <myNumber>23,79879-</myNumber> or other
and I can't turn the formatting off.
The assumption is that the formats are "standard", normally used for localization.
I'm looking to do something like:

convertStringToNumber(numberString, formatString, slignPosition)
3
  • 1
    Will numbers have a thousands separator as well as a decimal separator? E.g., could there be -2,123.79879 (, = thousands separator) or -2.123,79879 (. = thousands separator)? If they can, then you'll have to make an assumption if only one of the symbols (, or .) is present and happens to be in a valid location for either (such as 1,234, which could be "one and 234 hundreths" or "one thousand two hundred and thirty-four"). Commented Apr 29, 2014 at 17:36
  • 2
    You have filed a bug report with the suppliers of the back-end application, right? :-) APIs should send data in an unambiguous, consistent format. Localized forms are for users to consume, not software. Commented Apr 29, 2014 at 17:39
  • 1
    I definitely filed a bug, but there is very little chance it would be fixed any soon :-( Commented Apr 29, 2014 at 19:31

2 Answers 2

1

Not sure what you mean with formatString, but maybe something like this?:

function convertStringToNumber(num){
    num=num.split(',').join('.');
    if(num.indexOf('-') ==num.length-1){
        num='-'+num.substr(0,num.length-1);
    }
    return parseFloat(num) || null;
}



console.log(convertStringToNumber("-23.79879"))
console.log(convertStringToNumber("23,79879-"))
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way to achieve this is to expose the parsing rule from the backend, which obviously knows the format. This can be done in many ways, but one easy way i am fond of is simply to break down all the moving parts of the format, define properties for each on an object and then expose that object to the parser.

The object could look something like this:

var opts =  {
  thousandsSeparator: ',',
  decimalSeparator: '.',
  negativeSign: '-'
};

Then pass that object into a parsing function like this:

function parseNumber(opts, str) {
      var isNegative = false;
        if(str.indexOf(opts.negativeSign) != -1) {
            isNegative = true;
            str = str.replace(opts.negativeSign,'');
        }
        var parts = str.split(opts.thousandsSeparator).join('').split(opts.decimalSeparator);
        var num = 1 * parts[0];
        var deci = 1 * parts[1];
        if(deci) num += deci /  Math.pow(10,parts[1].length);
        return isNegative ? -1 * num : num;
}

You would then call it like thuis:

parseNumber(opts,'2,345.234-'); //-2345.234

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.