-4

I was tried to convert a string to float and I was tried toFixed but can't. How can I fix that problem? please Help me!!!

`

const str = "1.00"
const num = parseFloat(str).toFixed(2);
console.log(typeof num);

output: string

  • `toFixed` returns a string – Konrad Aug 20 '23 at 06:38
  • 1
    There is no float in js, there is just `number`. If you want it to be displayed in a custom way, you have to use a string – Konrad Aug 20 '23 at 06:39
  • 2
    What exactly are you trying to achieve? Are you trying to control the number's precision or the way it's printed? – Mureinik Aug 20 '23 at 06:40
  • There is extensive discussion of using JS Math to achieve this sort of thing at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor – A Haworth Aug 20 '23 at 06:58
  • 1
    It's the same problem with leading or trailing zeros. Numbers don't have a format. They only store the value. 1 and 1.00 have the same value. – jabaa Aug 20 '23 at 09:07
  • Welcome to Stack Overflow! Please [edit] your post to account for the questions and observations in the above comments. Good luck! – jcalz Aug 20 '23 at 15:41

0 Answers0