There is a number 256.52 , how to get the remaining number after the point?

 var number = 256.52; console.log(parseInt(number)) // тут получили целое число 

  • What result is expected in the end? - Grundy
  • @Grundy result 52 - Puvvl
  • And after the point there can be any number of numbers or only two? - Grundy
  • @Grundy, I apologize for the long answer, after the point there can be how many nice figures. - Puvvl

2 answers 2

 function fractionAsInt(value) { var str = ("" + value).split(".")[1]; return str? +str : 0; } console.log(fractionAsInt(256.52)); console.log(fractionAsInt(-256.52)); console.log(fractionAsInt(256)); 

  • The first option with negative does not work in the current version - Vladimir Klykov
  • @ Vladimir Klykov Thank you, I know. The task is underdetermined, as usual. - Igor
  • @Igor a little wrong, you need to get not 0.52 and 52 - Puvvl

You just have to take the remainder of the division by one.

However, it is worth noting that, since real numbers are not always exactly representable, some insignificant deviations may occur, i.e., before showing the user the number should be rounded to the desired number of characters:

 console.log(256.52 % 1) console.log(-256.52 % 1) console.log(.1 + .2) console.log((256.52 % 1).toFixed(3)) console.log((-256.52 % 1).toFixed(3)) console.log((.1 + .2).toFixed(3))