madhu
madhu

Reputation:

format the number

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;

Upvotes: 0

Views: 365

Answers (4)

Marcus Stade
Marcus Stade

Reputation: 4984

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

Upvotes: 0

Damovisa
Damovisa

Reputation: 19423

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*;

Upvotes: 0

lc.
lc.

Reputation: 116458

Integer divide by 1000.

Upvotes: 1

Jacob Poul Richardt
Jacob Poul Richardt

Reputation: 3143

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));
}

Upvotes: 0

Related Questions