Input field value - remove leading zeros

前端 未结 8 1834
南笙
南笙 2020-11-27 06:34

I have a textbox in Javascript. When I enter \'0000.00\' in the textbox, I want to know how to convert that to only having one leading zero, such as \'0.0

相关标签:
8条回答
  • 2020-11-27 07:12

    It sounds like you just want to remove leading zeros unless there's only one left ("0" for an integer or "0.xxx" for a float, where x can be anything).

    This should be good for a first cut:

    while (s.charAt(0) == '0') {            # Assume we remove all leading zeros
        if (s.length == 1) { break };       # But not final one.
        if (s.charAt(1) == '.') { break };  # Nor one followed by '.'
        s = s.substr(1, s.length-1)
    }
    
    0 讨论(0)
  • 2020-11-27 07:15

    Try this:

    <input type="text" onblur="this.value=this.value.replace(/^0+(?=\d\.)/, '')">
    
    0 讨论(0)
提交回复
热议问题