0

I got this issue. I want to convert integer 71 (type number) to one decimal, meaning 71.0

but I need it to stay of type number. Searched and the only solution I found was to use toFixed(1) and then parseFloat on the result, and that does returns a number but 71, without the decimal.

const float = (num) => {
    let fixed = (num).toFixed(1)
    let float = parseFloat(fixed)
    
    return float
}

float(71)

How should I do it?

3
  • 7
    "meaning 71.0 but I need it to stay of type number." not possible. Numbers in JavaScript do not have a format attached to them, 71 is the same as 71.0, which is the same as 71.00, etc. JS truncates unnecessary zeroes for display. If you want a specific format, you need a string. Commented Feb 25, 2021 at 14:51
  • 5
    The question doesn't really make sense. As a number, 71 and 71.0 are exactly the same. Commented Feb 25, 2021 at 14:51
  • 2
    A bit more depth: the number type in JavaScript is a "double-precision 64-bit binary format IEEE 754" value. (That applies to all numbers, even those that appear to be integers.) 71 and 71.0 are the same in this format. Commented Feb 25, 2021 at 15:00

2 Answers 2

1

This makes no sense, because an integer (whole number) will almost always equal its floating-point equivalent, unless there is a some odd precision behavior.

  • 71 === 71.0
  • 71 === 71.00
  • ...
  • 71 !== 71.000000001

Did you want to truncate a floating number using precision?

const truncateFloat = (n, p = 0) => parseFloat(n.toFixed(p));

// Truncate an irrational number (PI)
console.log(truncateFloat(Math.PI, 2) === 3.14) // true

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the parseFloat(fixed) and keep it as below

const float = (num) => {
    let fixed = (num).toFixed(2)
    //let float = parseFloat(fixed)
    
    return float
}

float(71)

use toFixed(n) where n is the number of 10th places after the decimal.

You have stated that you need to keep it as number at the end but the thing is, if the decimals are 0, it will always round up to a whole number. So you may want to rather consider adding the toFixed(n) at the point of printing to the screen so they always print with that extra.

========= UPDATE

I just found a cleaner solution. consider the below with a link to the solution

const float = (num) => {
    tmp = '0.00';
    tmp = (+tmp + num).toFixed(2);
    
    return tmp
}

float(71)

reference: how to get a float using parseFloat(0.00)

2 Comments

I just updated my answer. am not sure why you want it to specifically remain a number cos at your point of calculation, you can always do a parseInt(number) eg: parseInt(71) and you still get a number for your calculation.
check out the next update. it's a way better solution. hope that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.