views:

135

answers:

4

Hi how to decrease the number of digits before decimal point in action script. for example 10000 means it prints only 10 and 12345=12; 90000=90;

+1  A: 

Integer divide by 1000.

lc
90000 / 10000 would be 9 not 90 ;) though I'm sure he gets the point.
Lillemanden
You're right. It's not 10000, it's 1000.
lc
A: 

Try:

var i: int = j / 1000

Unless of course you meant decrease the number of digits before the decimal point, but keep the <1 amounts?

So: 10000*.123* means it prints only 10.123 and 12345*.8*=12*.8*; 90000*.999*=90*.999*;

Damovisa
90000 / 10000 would be 9 not 90 ;) though I'm sure he gets the point.
Lillemanden
Heh, I made the same mistake as lc it seems :)
Damovisa
yeah, found that that a bit funny as well.
Lillemanden
A: 

It depends a bit on what kind of numbers you handling (uint, int or Number) and if you want to reduce the digits by a set amount or to a set amount. And in the case of Number, how you want to handle digits after the decimal point. The code reduces uint to a set amount of digits.

trace(ReduceToDigitLength(10000, 2)); //traces 10
trace(ReduceToDigitLength(12345, 2)); //traces 12
trace(ReduceToDigitLength(90000, 2)); //traces 90
trace(ReduceToDigitLength(1235, 2)); //traces 12
trace(ReduceToDigitLength(15, 2)); //traces 15
trace(ReduceToDigitLength(9, 2)); //traces 9
//...
function ReduceToDigitLength(value:uint, length:uint):uint
{
    var digitLength:uint = value.toString().length;

    if (digitLength <= length)
     return value;

    return RemoveDigits(value, digitLength - length);
}

function RemoveDigits(value:uint, digitsToRemove:uint):uint
{
    return Math.floor(value / Math.pow(10, digitsToRemove));
}
Lillemanden
A: 

To reduce the number of digits before the decimal point, simply divide by a multiple of 10. If this is indeed a number and you want an integer, you may wish to floor the number by casting to an int or uint (depending on the nature of the number itself). Note that Math.floor will return another Number and won't do you much good as it may still produce output with very small fractional bits.

For greater formatting control in MXML, check out the formatting classes in the framework: http://livedocs.adobe.com/flex/3/langref/mx/formatters/NumberFormatter.html

macke