Is it possible to return a float value with 2 decimal places to end 0?

Asked

Viewed 453 times

0

I am pushing to be fed in Google Analytics and one of the guidelines that the prices are returned as float, so far all well managed to solve using parseFloat(), however, they need two decimals up to values ending with 00. Ex:(100.00 or 208.00). When returns a float value it removes values ending with 0.

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

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

Anyway, I have researched in several places and all the solutions I found and those I know returno string. I believe that technically it is not possible. But I would like a certainty.

They would tell me if it is possible to return a value with two decimal places to final values . 00.

Just to make clear the parseFloat Return a value with floating point as 100.99, now for a 100.00 no.

Thank you.

  • And what is the problem of sending the string with the decimal part reset? Anyway, regardless of the type you use, Google will receive as string, because the data is sent via HTTP.

  • The reason is just a request from the analyst who takes care of Google Analytics, as I have no knowledge of the tool, I have these doubts. The same reported that if I send a value as a string to Analytics it does not recognize.

  • Either it’s float or it’s value ended with zero. It can’t be both. If you explain the real problem, maybe you can help (what is in the question is an artificial requirement, google Analytics does not require "numeric type" with zeros on the right). Read this to understand it better: What is the "X Y problem"

  • The float is nothing more than the floating point. My doubt is very clear: "They would tell me if it is possible to return a value with two decimal places to final values . 00." I need to convert a string to float and if the value is 100 it needs to return to 100.00 and continue to float. What if that’s possible? Just.

  • 1

    Only as string. Numerically there is no difference between 100.00 and 100. In this case, the decimals are merely visual and therefore demand to be string.

  • Yes, your doubt is very clear. Only it stems from a lack of understanding on your part. That’s what we’re trying to explain. Let me try to answer it another way: Numerical formatting is only done in string, either directly or indirectly. I hope now you’ve been able to answer your question.

  • 1

    I agree, numerically it makes no difference. I just need to explain to myself technically that this is impossible.

Show 2 more comments

2 answers

1


Try this as proof

        var product_price = '100.00';
        var value = Number(parseFloat(product_price).toFixed(2)); 
        
        console.log( value );
        console.log(Number(parseFloat(value).toPrecision(5))); 
        console.log( value - 15); //operaçoes aritmeticas funcionam
        console.log( value + 15);
        console.log( value + 0.01);
         
        //se quiser fazer somente se a float for .00 e nao , por exemplo 100.01
        product_price = '100.01';

        if(product_price % 1 == 0){
         var value = Number(parseFloat(product_price).toFixed(2)); 
        }else{
         value = parseFloat(product_price);
        }
        console.log(value);
        
        
So yes it is not possible, In JS there are no ints nor float are even of type Number, I hope it serves as proof, but should not prevent operations in any way

Beware of numbers in JS (EN)

-1

        var product_price = '100.00';
        var value = parseFloat(product_price).toFixed(2); 
        console.log( value );

In case to keep everything you’ve done and add the method toFixed(x) with the number of decimals you wish to have after the point.

  • The toFixed() method returns as String. I mentioned this same method in the text.

  • 4

    It is. The answer makes no sense: take a string, convert to float, then convert to string start again. Three lines of code to arrive at the same starting value.

  • I believe that it is not really possible to send the value as float and that it is with this format 100.00.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.