Round floating point to integer


I have a simple timer setup

in initialize

    this.timeperlevel = 61;

in update

    this.timeperlevel -= dt;

I am showing the number in a ui text element, but the problem is that it shows a floating number, I need to convert it to integers like with Math.Round(), which is not included in the PlayCanvas API.

As the main project is private i have made a simple project containing just the timer:

You can round/floor/ceil the float number before assignning it to the text element. As it’s a timer count down, I would suggest using ceil.

How would i do that? i don’t see any of those function in Math Api

like with Math.Round(), which is not included in the PlayCanvas API.

It’s not in the PlayCanvas API, but it’s still available in JavaScript, so there’s no issue using it in your projects.

1 Like

Oh! I had no idea i could acces common Javascript functions! Thank you!, but could you provide a code snippet? Every configuration i have tried for this problem returns NaN.

Untested but you get the idea

this.timeperlevel -= dt;
if (this,timeperlevel < 0) {
    this.timeperlevel = 0;
someTextElement.text = Math.ceil(this.timeperlevel).toString();

Still return NaN, but i get the idea, i’ll tinker some more with it.

Thank you all for your help! You are all fantastic people! Have a really nice day/weekend!

The mistake was somewhere else in the code, i didn’t realize that when i only tested that particular screen it did not define this.timeperlevel = 60, it only did so when it was redirected from another scene. The code you gave works perfectly!

So @alexrojae i have my code saying this:

EHealth.attributes.add('textHealth', {
    type: 'entity'

 this.textHealth = 100;
if ( {
    scale.x = pc.math.lerp(scale.x, 0, 0.7);  // SCALE DOWN
    this.textHealth -= 20; 

would this work? well it doesnt but how would i make it work? i tried looking at your example above but pops up private

Why are you setting an entity attribute to a number type?
Where are you setting the value to a text element?

i did this so when i connect the textHealth attribute to the text entity i can tell it that thats the entity of text i want to change

Im doing this so it knows where to start(even though its set to the number) and where to end

That’s fine expect for the fact that you are overwriting that variable with a number type. You are effectively doing this:

this.textHealth ="SomeTextElementEntity");
this.textHealth = 100;

so instead of
this.textHealth ="SomeTextElementEntity");
i just leave this.textHealth = 100
and keep the attribute?

Let’s try this again. Show me the line of code that is setting the value to the text element so you can see the number on screen. here id the full code,

EHealth.prototype.initialize = function() {
    this.healthEntity ='eHealth');
    this.AI ='AI');
    this.textEntity ='win');
    this.textHealth = 100;

im setting the value here
Ive tryed this this.text.element.text = “”; to kinda help but never succeds (i think because it needs a string not a number

I don’t see where you are setting the textElement text in your code.

i thought by doing this:
this.textHealth = 100; was setting the value?

Not to a textElement that isn’t.

i have tryed doing this.textHealth.element.text but alsways calls element undefined, and ive tryed using pc.ELEMENTTYPE_TEXT but that diesnt work neither if neither one of those are right i have no idea what to do next

ok so i did what ive seen used before i did this:

EHealth.attributes.add('textHealth', {
    type: 'entity'
    this.textHealth ='textHealth');
 this.entity.element.textHealth -= 2;

at first i just did this.entity.element.text but called text undefined and i think its because i have something else named text
EDIT: Nothing else is called on as text
EDIT #2:
ok so i got the text to change but now what i want it to do is subtract 2 numbers every time ill keep trying thanks for helping out btw