ParseFloat does not work on mobile devices

I am writing a very simple html calculator. The end result and user inputs are displayed in the "textDisplay_" area below:

<div id ="textDisplayRow_"> <div id = "textDisplay_" class = "calcTextDisplay">0.0</div> </div> 

I also have JavaScript code that receives and parses the displayed value in a float using the following functions:

 function getCurrentText() { "use strict"; return document.getElementById("textDisplay_").innerHTML; } function getDisplayedNum() { "use strict"; if (getCurrentText() === emptyMesg) { return maxSafeInt; } var t = 1*getCurrentText(); //var t = parseFloat(getCurrentText(), 10); if ((isNaN(t)) || (t === undefined)) { return 0; } else { return t; } } 

This code works great on desktop browsers (I tested it on Chrome, FireFox, and IE). They all look good and say if I enter: 3.14 * 2 and press '=', they give me 6.28.

However, on my Android phone 4, using the latest versions of mobile chrome and firefox, all decimal numbers are rounded up! so if I hit "3.14 * 2 =", I get "6"!

How can I fix this problem? I looked at other posts containing parseFloat not working, and none of them look like this problem. Also, both give similar results:

 1*getCurrentText(); parseFloat(getCurrentText(), 10); 

One option is to write your own parseFloat (I previously did this for the microcontroller!), But I really don't want to go that route.

Thank you very much in advance.

+1
javascript mobile-chrome
Apr 05 '16 at 23:02
source share
2 answers

You can try:

 var t = 1.0 * getCurrentText(); 

to get a float.

0
Apr 05 '16 at 23:23
source share

I am sure that your problem is not what you think. Visit this page on your desktop and mobile browser. For me they give the same results.

https://jsfiddle.net/xpkhqdLa/1/

With str being a string containing a floating point number, all below produce the same correct floating point value:

 var values = [ str*1, 1*str, 1.0*str, parseFloat(str) ]; 
+1
Apr 6 '16 at 17:26
source share



All Articles