search

Home  >  Q&A  >  body text

javascript - Ask the regular expert, if the search starts with a decimal point, add 0 in front of the decimal point

Cause of the problem:
Project environment php+oracle, if the data taken out from oracle is a number less than 1, the result will be 0, such as: '0.8', and the result will be displayed on the page It becomes '.8'. After reading Oracle's tutorial, I found that the solution is to_char before taking the value. However, because there are too many fields, it is very troublesome to do this. I plan to replace it with regular expression.

Example:
.5=>0.5
.03=>0.03

迷茫迷茫2827 days ago499

reply all(5)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 13:02:39

    $number = '.5';
    $number = preg_replace('/^(\.\d+)/', '0$1', $number);
    echo $number;

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:02:39

    0+.5 will become 0.5, the simplest way I can think of

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:02:39

    $num = ".8";
    $res = preg_replace('/^.(d+)/', '0.${1}', $num);
    The obtained $res is just

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:02:39

    reg = /^\./;
    var arr = ['.5', '.03', '4']
    for (let i = 0, len = arr.length; i < len; i++) {
        console.log(arr[i], arr[i].replace(reg,'0.'))
    }

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:02:39

    Direct var_dump((float)$str);

    reply
    0
  • Cancelreply