0

I have a float x that can be any size and need to increment its amount by 1 on the last zero, so .1 for 1.0, .01 for 2.00, etc.

The following code works for certain numbers, but not all:

Javascript

let a = "0".repeat(floatNumber.length-2)+'1';
let position = floatNumber.indexOf(".");
let add = [a.slice(0, position), ".", a.slice(position)].join('');

1.1 becomes 1.2.

but

10.1 becomes 10.20. I need an output of 10.2.

Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
matt
  • 13
  • 2
  • Do you need output like 10.2 rather than 10.20? – Shivani Sonagara Jul 01 '19 at 05:03
  • 2
    `jquery` and `math` tags? [Really?](https://i.stack.imgur.com/ssRUr.gif) – VLAZ Jul 01 '19 at 05:05
  • @VLAZ It has nothing to do with jQuery, but math seems appropriate. – JLRishe Jul 01 '19 at 05:07
  • @JLRishe (that was the joke) – VLAZ Jul 01 '19 at 05:07
  • @VLAZ Ah, I didn't notice the link. – JLRishe Jul 01 '19 at 05:08
  • I still don't get it, `increment its amount by 1 on the last zero` then `.1 for 1.0` and `.01 for 2.00` and `10.1 becomes 10.2` seem to me like 3 different things. so which one is it? – Thomas Jul 01 '19 at 05:11
  • When there is a dot, the length gets increased. – Jonas Wilms Jul 01 '19 at 05:23
  • @JonasWilms so in other words, when there is a dot, you simply append the digit `"1"` to the number? That doesn't sound right. My issue here is, what is the difference between `2`, `2.0`, `2.00` and for example `0000002` or `2.000000`? mathematically they are all the same value/float so what is the last zero in these? If `2.00` is supposed to be incremented by `0.01` then why is `10.1` not incremented by `1.0` as there is the last `0` I can see? Or by `0.01` as this would be the first zero at the end. Why is it supposed to be `10.2`? I don't get the rules. – Thomas Jul 01 '19 at 05:35
  • What happens for "0.09"? "0.10"? "0.1" something else? – Kaiido Jul 01 '19 at 05:42
  • Dood's answer is exactly what i needed. @VLAZ; sorry my first post on stackoverflow, added jquery tag as i am using the library in the program anyway. – matt Jul 01 '19 at 07:00

2 Answers2

1

You could just take + increase the last digit:

    const result = input.slice(0, -1) + (+input[input.length - 1] + 1);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

I tried your code, it is working fine to add I used parseFloat , so I guess may be you are missing it as you haven't added that part of code.

MODIFIED:

I have modified the snippet as it didn't support some of the numbers. now it will give your desired output..

function floatNum(floatNumber){
  
  let position = floatNumber.indexOf(".");
  if(position == -1){
    return +floatNumber+1;
  } else {
    let len = floatNumber.length;
    let a = "0".repeat(len-2)+'1';
    let dec = len-position-1;
    let add = [a.slice(0, position), ".", a.slice(position)].join('');
    return (parseFloat(floatNumber) + parseFloat(add)).toFixed(dec);
  }
}
console.log(floatNum('0.01'))
console.log(floatNum('10.12'))
console.log(floatNum('1'))
console.log(floatNum('123'))
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59