简体   繁体   中英

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;

整数除以 1000。

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM