0

How could I convert a string "0.0" into a float 0.0? Is there some way? Currently if I do:

parseFloat(0.0)

It returns a number 0.

If I do :

parseFloat("0.0").toFixed(1)

It returns a string "0.0". Is there a way I could get a number type with value 0.0 ?

Amanda
  • 1,823
  • 1
  • 15
  • 45
  • Float `0` and `0.0` or `000.000` are all same and will be represented by a `0` so long as they are floats. – Nijraj Gelani Jun 11 '19 at 06:48
  • 2
    There is no number with value of `0.0`. JS has a single numeric format and all numbers are that. There are no integers and floats. Also, there is no presentation included with the number, which is what you're asking for - a number is a number and will always be formatted to the same thing when presented. If you want something different, you need a string. – VLAZ Jun 11 '19 at 06:49
  • Related: https://stackoverflow.com/questions/7312693/how-to-convert-int-number-to-double-number/7312732#7312732 – VLAZ Jun 11 '19 at 06:55
  • @Amanda if you are converting 0.0 it will return 0 only because 0.0 and 0 are same – Dominic Amal Joe F Jun 11 '19 at 07:28

4 Answers4

0

In Javascript, 0.0 is the same as 0. If you want 0.0, it will have to be a string.

For all your calculations, this should not make any difference. It is not uncommon to format numbers right before rendering them in your view.

JasperZelf
  • 2,576
  • 1
  • 21
  • 31
-2

In javascript float doesn't exist like a type. (like C/C++)

You can create just a number type 0.0, 0, -1.1

interpreter of language engine choose by self which type do you need and did this without your control.

In js you can use only this types of variables

  • Boolean
  • Null
  • Undefined
  • Number
  • BigInt
  • String
  • Symbol (new in ECMAScript 6)
  • Object

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types

Vadim Hulevich
  • 1,719
  • 7
  • 16
-2

You can use this,

parseFloat(parseFloat("0.0").toFixed(1))
-3

You can use parseFloat(« 0.0 ») to convert string in float It will take the first number and when the next char is not a point or a number

BootEXE
  • 11
  • 2