2

I'm doing a push to be fed into Google Analytics and one of the guidelines that prices are returned as float, so far so good. I solved using parseFloat (), however, I have to return two decimal places for values that end with 00. Example: (100.00 or 208.00). When it returns a float value it removes values that ends with 0.

var product_price = “100.00”; value = parseFloat(product_price);

Result with float= 100

I used the .toFixed (2) method to place the two decimal places but return as String.

Well, I already researched in several places and all the solutions that I found and those I know will return a string. I believe that technically it is not possible. But I'd like to make sure it's really not possible.

Saberiam me dizer se é possivel retornar um valor com duas casas decimais para valores de final .00.

To clarify the parseFloat return a value with floane point like 100.99, now for a 100.00 not.

Sorry to write the question in Portuguese before.

Thanks

Pac0
  • 21,465
  • 8
  • 65
  • 74
A. G. Araujo
  • 333
  • 1
  • 2
  • 6
  • Don't necessary close my topic. I translater for you Quentin. – A. G. Araujo Oct 11 '18 at 15:00
  • 1
    @Veverke I rewrite now. – A. G. Araujo Oct 11 '18 at 16:02
  • Is it poossible to know *why* the 'string' part is not satisfactory for you ? This looks like a [x/y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Pac0 Oct 12 '18 at 03:46
  • You researched and found out it is not possible. Guess what it is not possible. JavaScript does not have significant digits with numbers. If you need them, it has to be a string. – epascarello Oct 12 '18 at 03:54

3 Answers3

2

A number value itself does not have the concept of decimal places until you want to print it. It means nothing for a number type to have fixed decimal places if you are not printing it out. Therefore, decimal places only come into play when you convert the number to String.

Whenever you want to "see" 100.00, you already mean to convert the number with value = 100 to a String with 2 decimal places for you to "see". You can only see String and never see a number.

Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
1

You are mixing two things : the value of a number, and how it is displayed.

100, 100.0, 100.00 (or even 1E2, 10E1, 0.1E3) are visual representation of the same numeric value.

I will put aside the fact that computer numerical values are not always the same as real numbers in a mathematical sense.

In JavaScript, parseFloat returns a numeric type. The numeric values are displayed by default without trailing zeros. That is, when you do console.log(100), the 100 is converted to a string, "100", and the same goes for console.log(100.00), the number is converted to a string... "100" as well.

But you can always specify a string representation by forcing a number of decimals to be displayed (that's what .toFixed() do).

See : Format number to always show 2 decimal places

You can simply use toFixed(2) on any numeric binding (variable) to achieve what you want.

let a = 100;
console.log(a.toFixed(2));

I used the .toFixed (2) method to place the two decimal places but return as String.

Yes, but actually any number you try to display is some kind of string at some point (think about console.log(100.00)). The internal value (numeric) for 100 and 100.00 is the same.

The only difference lies in their string representation, so asking for a way to do that "without string" is contradictory.

Pac0
  • 21,465
  • 8
  • 65
  • 74
0
    <!DOCTYPE html>
    <html>
    <title>Web Page Design</title>
    <head>
    <script>
    function sayHello() {
        var num = "100.00";
        var value=parseFloat(num);
        var n = value.toFixed(2);
        alert(n);

    }
    sayHello();
    </script>
    </head>
    <body>
    </body>
    </html>
Bhaskar Reddy
  • 171
  • 1
  • 9