Score in integers?

I’m making a website where it would be nice to have the user see the X-axis and Y-axis of his mouse :mouse: , and I have that part coded easily with no trouble :smile: , but the problem :bomb: is that the :x:-axis and Y-axis are shown in ugly :nauseated_face:, and too long :yawning_face: decimals. Is there a way to force the data to be in integers, or just two decimal digits at least?

Thanks either way,

~Hanzoh Alani

hi @Hamzah_Alani
I have used this before to convert float to integer
Math.round(2.7);
answer = 3
Math.round(2.1);
answer = 2
or if you want just the whole number use this…
Math.trunc(8.76);
answer = 8

Hope this helps
Greg

1 Like

seems like num.toFixed(x) also works. x is how many decimal places you want to keep, and I think it converts the number to string form. so 3.234.toFixed(1) should return "3.2". Since all you want to do is list coordinates, @Greg’s way should work just fine, but in the future if you want to round things to other places you can use this. (I’m actually not sure if Math.round() lets you do the same thing, but this is what I have.)

2 Likes

Thanks @Greg and @awc95014 for the quick answer, I tested out both methods. I think Greg’s code would work for a single number, but I had trouble in how to plug in @awc95014’s code- it would be great to know what I’m doing wrong… here’s how the code and the results look like:
Untitled_ Jun 5, 2020 1_21 PM

Typed sample of my code

project.score=scoreCounter.x;
scoreCounter.setTest("x: "+project.score);
scoreCounter.x=mouseX;
project.score=mouseX.toFixed(5);
//I didn’t use copy paste so it might have typo’s
//BTW The text is named scoreCounter

1 Like

@Hamzah_Alani with the order of the lines of code project.score is not yet truncated when you setText:
project.score=scoreCounter.x; //NOT TRUNCATED
scoreCounter.setText("x: "+project.score);

You could write it like this instead:
scoreCounter.setText("x: "+scoreCounter.x.toFixed(5));
scoreCounter.x=mouseX;
project.score=mouseX;

2 Likes

Works like magic! My math is pretty weak,
but thanks @nick and everyone else, and
sorry for any trouble

2 Likes