# Math.round() and forcing flash to show an ending zero

Let me preface this with the fact that I am new to AS3 and currently reading a few books on it and designing flash with the GUI of Adobe FL Pro CS4 & CS5.

So I coded a simple preloader:

``````stop()

gotoAndPlay(2);
}

}
``````

I use the Math.round to show the first decimal place in the percentage just to give it the feel of moving a bit faster, but as the tenth place cycles 0-9 the 0 will not display and I am unaware of how to force it to display it. Since the 0 does not display it makes the loading percentage text jump from left to right slightly as it loads. I would like it to display the 0 as it cycles so there is consistency in the display and no jumpyness.

Any help or code snippets would be much appreciated. Thanks!

``````
loadText.text=Math.floor(perc * 100) + '.' + (perc-(int)perc)*10 + '%';

``````

(not tested)

Thank you for your response and ideas.

``````perc=loaded/total*100;
loadText.text=Math.floor(perc * 100) + '.' + (perc-(int)perc)*10 + '%';
``````

(not tested)

I don’t see your logic in multiplying perc by 10000 :x but I see what you’re getting at:cool:. Though I do not think this will work because Flash will place a decimal in the (perc-int(perc))*10 and a decimal before that in your code…?

``````var percent:Number = loaded/total*100;
loadText.text = Math.floor(percent) + (percent - int(percent)) + '%';

``````

?

I think this will end up giving the sort of readout I want BUT displaying trailing values after the decimal point which defeats the purpose of typing all that jazz… I could instead just code:

Is there a Math.roundTo function?? it throws an error when I try.

Any ideas?

No there is no roundTo function. I was looking if there was a toFixed. There is in Javascript and since javascript and AS are both derived from ECMA script … but alas.

I did however find .toPrecision (Number (ActionScript 3.0)). Maybe that’ll help?

Thank you for your help it seems toPrecision might be able to accomplish what I want.

Yea it seems that roundTo and toFixed are both AS2. toPrecision should work with a few if statements… maybe?:

``````if (percent >= 100) {
} else if (percent < 100 && percent > 9) {
} else {
}
``````

Think that will work? I’m not too sure about how if else loops execute in the workflow…

``````
loadText.text = percent.toPrecision( String(int(perc)).length + 1) + "%";

``````

Your code is fine btw, this is just shorter

I like :nanaman: very to the point :teach:

however because I am ridiculous about looks (and the site is going to advertise and sell services) I would still include an if statement to get rid of the hundredth place when perc < 0.99 :crazy:

for now I will do what I understand:

``````stop()

if (percent >= 100) {
} else if (percent < 100 && percent > 9.99) {
} else if (percent < 10 && percent > 0.99){