0

Why is different result in variable "pisa" in this script ? When I use ".toString" is not the same value like when I write value in quotation mark. I need value like in second script. Thank you very much.

// first script
var cez = 45.30;
var bar = (cez).toString();
var convertedTime = (Number(bar.split('.')[0]) * 60 + Number(bar.split('.')[1])) * 60000;
var pisa = convertedTime;
console.log(pisa); // 162180000

// first script
var bar = '45.30';
var convertedTime = (Number(bar.split('.')[0]) * 60 + Number(bar.split('.')[1])) * 60000;
var pisa = convertedTime;
console.log(pisa); //163800000
popx99
  • 7
  • 4
  • I would say that in first case `bar.split('.')[1]` returns 3, and in second case it returns 30 – gawi Aug 09 '17 at 10:17

1 Answers1

4

The difference is that doing (cez).toString() will truncate the last 0 so you'll get 45.3 and bar.split('.')[1]) will be 3 instead of 30 in the first case

Mihai Pantea
  • 360
  • 3
  • 8
  • Aha, thank you. I understand. It is possible solve this problem with "0" ? – popx99 Aug 09 '17 at 10:23
  • You could use toFixed() as explained here: https://www.w3schools.com/jsref/jsref_tofixed.asp. Given your context, you should however do this only if the second decimal is `0` otherwise, you'll incur rounding. Checkout https://stackoverflow.com/questions/4187146/display-two-decimal-places-no-rounding – Mihai Pantea Aug 09 '17 at 10:25
  • Thank you very much, this solve my problem. I use "(45.30).toFixed(2)". Thank you :) – popx99 Aug 09 '17 at 10:32