PDA

View Full Version : Expressions/Javascript Rounding


graymachine
07-25-2006, 08:23 PM
I am familiar with Math.round to round numbers.. and also using Math.round(X*10)/10 to round to, in this case, tenths.

What I notice, however, is that when using the latter example, integer numbers exclude the ".0". That makes sense. But, aesthetically, I want the .0 left there to be visually consistent.

Does anyone know of a way to do this?

graymachine
07-25-2006, 11:12 PM
I am familiar with Math.round to round numbers.. and also using Math.round(X*10)/10 to round to, in this case, tenths.

What I notice, however, is that when using the latter example, integer numbers exclude the ".0". That makes sense. But, aesthetically, I want the .0 left there to be visually consistent.

Does anyone know of a way to do this?


Figured it out. You have to test for the condition and then add a ".0" on the end, like this:


check = x % 1
if (check == 0){
"" + x + ".0"
}else{
x
}

beenyweenies
07-27-2006, 05:27 AM
Figured it out. You have to test for the condition and then add a ".0" on the end, like this:


check = x % 1
if (check == 0){
"" + x + ".0"
}else{
x
}


uhhhhh uh uhu hhuhuh huh, could you like, order less stuff? ;)

graymachine
07-27-2006, 05:49 AM
uhhhhh uh uhu hhuhuh huh, could you like, order less stuff? ;)


Heh. .it's not that bad. take a look:

check = x % 1

Declares "check" to be equal to the remainder of x divided by 1. This is called the "modulus" operator. Simply put, it gives you the remainder after dividing two numbers.

if (check == 0){

if that variable, check, is equal to 0 (as in, it is an integer number like 5 )

"" + x + ".0"

then create a text string of x plus the characters ".0". The "" says 'Hey AE, we are about to start adding some text strings together, ok?"

Also, it could be useful here to substitute this next part to convert the text string back to a numeric value:

n = "" + x + ".0"
n.value


}else{

if check is not equal to zero (it is a non-integer number like 24.5)

x
}

then pass X unaffected and close the if/else condition

CGTalk Moderation
07-27-2006, 05:49 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.