0

I have created a format that allows two digits after the decimal, for example 99.99 or 55.55 etc. I would like to make it allow 99.999 instead of two. Not sure where the problem is.

function formatNumber (nStr) {
    nStr = String(nStr)
    nStr = nStr.replace(/\,/g, '')
    nStr = trimNumber(nStr)

    if ($.isNumeric(nStr)) {
        nStr = String(parseFloat(nStr).toFixed(2))
        nStr += ''
        x = nStr.split('.')
        x1 = x[0]
        if (x.length > 1) {
            if (x[1].length > 2) {
                var thirddigit = x[1].substr(2, 1)
                var addone = false
                if (Number(thirddigit) >= 5) {
                    addone = true
                }

                x[1] = x[1].substr(0, 2)
                if (addone) {
                    var y = Number(x[1]) + 1
                    x[1] = y
                }
            }
        }
        x2 = x.length > 1 ? '.' + x[1] : ''
        var rgx = /(\d+)(\d{3})/
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2')
        }

        if (x.length > 1) {
            if (x[1].length == 1) {
                x2 = '.' + x[1] + '0'
            }
            if (x[1].length == 0) {
                x2 = '.00'
            }
        } else {
            if (x1 != '') {
                x2 = '.00'
            }
        }

        if (getLeft(x1 + x2, 1) == '.') {
            return '0' + x1 + x2
        } else {
            return x1 + x2
        }
    } else {
        return ''
    }
}
Antti29
  • 2,953
  • 12
  • 34
  • 36
Tr Ex
  • 61
  • 1
  • 6
  • 4
    your code is so much overcomplecated. can you describe in a few sentence what your function should do? I'm pretty sure I can show you a solution in one or two lines that does the same. || BTT: `toFixed(2)` has to be changed to `.toFixed(3)`. But again: the code is... ... not good. – Joshua K Oct 26 '17 at 22:44
  • It is very difficult to understand the code. Please review https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places – Telavian Oct 26 '17 at 23:04

1 Answers1

0

I found the solution! I posted it in case anyone would have the same scenario and needs it.

function Format3DigitDecimal(e, thisobj, min, max) {
        var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
        var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))

        var inStr = $(thisobj).val()




        if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
        {
            if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
            {
                ret = false
            }
        }

        if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
        {
            if ((inStr.length == 3) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
            {
                ret = false
            }
        }

        return ret
    }
Tr Ex
  • 61
  • 1
  • 6