Jump to content
Sign in to follow this  
Notretsam

javascript rounding number contained in var up

Recommended Posts

Am more familiar with PHP than Javascript,  so not sure how to fix this strange bug.

basically have a wrestling game where people can do matches against other people,  naturally I have a match list that person can view there current active matches and click to go to match.  

I use a onclick event, which contains the $matchid, then the following javascript 

 

<script type="text/javascript">
function matchshow($sids) {
var x=$sids;
var cstyle = document.getElementById('fcstyle').value;
window.location.href = "match.php?sid=" + x + "&cstyle=" + cstyle;
}
</script>

Don't need to worry about the cstyle, that is the current tab the person is viewing in match.

The var x=$sids is what contains the match id number.

Now I just recently moved hosting companies, and the match list was working fine on old host and new host, but all of a sudden today it stopped working.

I managed to figure out why, but just don't know how to fix it.

actual=151636871863316991
showing=151636871863317000

actual=1516368718560392395
showing=1516368718560392400

The actual is the actual match id,  the showing is what javascript is sending across in the window.location.href, its the last 4 digits that are wrong and appears it rounding the figure up.

I have no idea why it all of a sudden doing this,  is there a way to prevent it from rounding the number up? 

I did just have to increase the length of the match id today, due to another bug I run into, so presume that is related to why this suddenly happening. 

 

Share this post


Link to post
Share on other sites

is there a way to prevent it from rounding the number up? 

You really think that Javascript is just arbitrarily rounding a number for no apparent reason?  I bet that's not the problem.  I bet the problem is that's the actual value that you're passing to that function, and you need to work backwards to figure out why if you don't think that's the correct value.

Otherwise, one issue might be that the number is bigger than what Javascript can represent as a number, so make sure you're treating that value as a string and not a number.  If you're just putting it into the URL and redirecting then you don't need to treat it like a number.

Share this post


Link to post
Share on other sites

well javascript is just magically rounding the number up as you can clearly see by my original post, where I have shown the actual/showing numbers.

I have already investigated why it occurring, I looked at view source and the form with the onclick event, I can clearly see the correct number in there.

The number is correct before the function shown in my original post, then during the javascript function, or the window.location event changing you to match.php page, the number is rounded up. 

I have read about javascripts limitations with numbers, and a friend mentioned using string.

I tried below and the onclick event wouldn't occur at all. 

<script type="text/javascript">
function matchshow(string($sids)) {
var x=$sids;
var cstyle = document.getElementById('fcstyle').value;
window.location.href = "match.php?sid=" + x + "&cstyle=" + cstyle;
}
</script>

I also tried var x = string($sids);

I don't know javascript very well , so maybe am using it incorrectly?   

Also when I manually type the url into browser with the actual number id,  the page works fine, so there no issues on match.php 

I really have exhausted every possible fix that I can think off, and already worked backwards,  which is why I identified the actual number for match id being incorrect, and it appearing too being rounded up.  

 

 

Edited by Notretsam

Share this post


Link to post
Share on other sites

Where is the function being called? You can't fix this from inside the function, you have to fix it in the code that's calling the function. The number is likely much too large for Javascript to represent. Converting it back to a string after the precision was lost is not going to recover the lost precision.

This is not valid syntax, so it's not going to work.

function matchshow(string($sids)) {

 

Share this post


Link to post
Share on other sites
<div class='contframeturn' onclick='matchshow(151636871863316991)'>

function is called in div opening tag,  

basically there is a mysql query that loops through database and outputs each match on a page,  during that the above id for match is attached to the onclick event

oh while I type this , am wondering if is it because I wrapped it with ' and not "  ?   

 

Share this post


Link to post
Share on other sites

No, the main problem is that the number is too large for Javascript to represent it correctly. Pass it in as a string instead by wrapping it in quotation marks, like this:

matchshow("151636871863316991")

Share this post


Link to post
Share on other sites
31 minutes ago, Ingolme said:

No, the main problem is that the number is too large for Javascript to represent it correctly. Pass it in as a string instead by wrapping it in quotation marks, like this:


matchshow("151636871863316991")

ok thanks,  its 8.46PM for me now, so shall be tomorrow before I can try.

I do echo the text out due to there being a variety of different match lists and options person viewing can select on page. 

 

echo "<div class='contframeturn' onclick='matchshow(&quot;$matchID&quot;)'>";

Presume the &quot;  won't work in the above echo line?  

Share this post


Link to post
Share on other sites

That will probably work, considering that HTML parses all entities before executing anything. Alternatively, you can just escape double quotes with a backslash.

echo "<div class='contframeturn' onclick='matchshow(\"$matchID\")'>";

 

Share this post


Link to post
Share on other sites

tested this morning and using &quot; worked

it is amazing that something so simple to do , is all that was needed to fix it lol

thank you for help Ingolme

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×