数字格式化程序:00 而不是 0

发布于 2024-10-11 15:06:38 字数 64 浏览 9 评论 0原文

我想格式化一个数字以显示 00 而不是 0 和 01, 02...10, 11,

I would like to format a number to display 00 instead of 0, and 01, 02...10, 11,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浪荡不羁 2024-10-18 15:06:38
function formatNumber(number) : String {
  if (number > 10)
    return number;
  else
    return '0' + number;
}
function formatNumber(number) : String {
  if (number > 10)
    return number;
  else
    return '0' + number;
}
双手揣兜 2024-10-18 15:06:38

也许这是我的假设,但您是否试图在某种时间显示中表示秒或分钟?对于 Flex,一种方法是使用 Date 对象和 DateFormatter。这样您就可以提供格式字符串来输出您想要的时间。

var seconds:int = 4;

var date:Date = new Date();
date.seconds = seconds;

var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "SS";

trace(formatter.format(date));

Perhaps this is presumptive of me but are you trying to represent seconds or minutes in some sort of time display? With Flex one way to do that would be to use the Date object and the DateFormatter. That way you can provide format strings to output your time however you want.

var seconds:int = 4;

var date:Date = new Date();
date.seconds = seconds;

var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "SS";

trace(formatter.format(date));
方圜几里 2024-10-18 15:06:38

您无法将数字格式化为具有前导 0 填充。但由于您需要此功能来格式化 NumericStepper 组件中显示的数字,因此您可以在每次值更改时简单地格式化输出文本字段中的文本。

我给你写了一个小例子。创建一个新的 AS3 FLA,将 NumericStepper 添加到舞台,将其实例名称设置为“stepper”。然后将Main.as设置为主类。

主要.as:

package  {
    import fl.controls.NumericStepper;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class Main extends MovieClip{

        public var stepper:NumericStepper;

        public function onStepperChange (ev:Event) : void {
            updateStepper();
        }

        private function updateStepper() : void {
            var num:String = stepper.value > 10 ? "" + stepper.value : "0" + stepper.value;
            stepper.textField.text = num;
        }

        public function Main() {
            updateStepper();
            stepper.addEventListener (Event.CHANGE, onStepperChange);
        }

    }

}

You cannot format a number to have a leading 0 padding. But since you need this functionality to format the number display in a NumericStepper component, you can simply format the text in the output textField every time the value changes.

I wrote a little example for you. Create a new AS3 FLA, add a NumericStepper to the stage, set its instance name to "stepper". Then set Main.as as the main class.

Main.as:

package  {
    import fl.controls.NumericStepper;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class Main extends MovieClip{

        public var stepper:NumericStepper;

        public function onStepperChange (ev:Event) : void {
            updateStepper();
        }

        private function updateStepper() : void {
            var num:String = stepper.value > 10 ? "" + stepper.value : "0" + stepper.value;
            stepper.textField.text = num;
        }

        public function Main() {
            updateStepper();
            stepper.addEventListener (Event.CHANGE, onStepperChange);
        }

    }

}
葵雨 2024-10-18 15:06:38

如果使用spark.components,则NumericStepper具有valueFormatFunction属性。
如果您使用 fl.controls,那么我认为上面在 NumericStepper 上使用 Event.CHANGE 的解决方案是最好的。

这就是我在使用spark.components.NumericStepper时的做法

var num_fld:NumericStepper = new NumericStepper();
num_fld.valueFormatFunction = numericFieldFormat;


private function numericFieldFormat(num:Number) : String {
    return((num < 10) ? '0' + String(num) : String(num));
}

If are using spark.components, then the NumericStepper has a valueFormatFunction property.
If you are using fl.controls, then I think the solution above that uses Event.CHANGE on the NumericStepper is best.

This is how I would do it when using spark.components.NumericStepper

var num_fld:NumericStepper = new NumericStepper();
num_fld.valueFormatFunction = numericFieldFormat;


private function numericFieldFormat(num:Number) : String {
    return((num < 10) ? '0' + String(num) : String(num));
}
尐偏执 2024-10-18 15:06:38

如果您在 Flex 3 中,您无法直接访问 inputField,但您可以通过使用 mx_internal

<mx:NumericStepper id="stpprHours" minimum="1" maximum="24" stepSize="1" creationComplete="formatStepper(stpprHours)" change="stpprHours_changeHandler(event)" value="1"/>

<mx:NumericStepper id="stpprMinutes" minimum="0" maximum="59" stepSize="1" creationComplete="formatStepper(stpprMinutes)" change="stpprMinutes_changeHandler(event)" value="0"/>

<mx:Script>
 <CDATA[[
        protected function stpprHour_changeHandler(event:NumericStepperEvent):void {
            formatStepper(stpprHours);              
        }

        protected function stpprMinutes_changeHandler(event:NumericStepperEvent):void {
            formatStepper(stpprMinute);                             
        }

        protected function formatStepper(stepper : NumericStepper) : void {
            if(stepper.value<10) stepper.mx_internal::inputField.text = "0" + stepper.value;

    }
 ]]>
</mx:Script>

if you are in Flex 3 you can't access directly to the inputField, but easy you can solve it by using mx_internal

<mx:NumericStepper id="stpprHours" minimum="1" maximum="24" stepSize="1" creationComplete="formatStepper(stpprHours)" change="stpprHours_changeHandler(event)" value="1"/>

<mx:NumericStepper id="stpprMinutes" minimum="0" maximum="59" stepSize="1" creationComplete="formatStepper(stpprMinutes)" change="stpprMinutes_changeHandler(event)" value="0"/>

and

<mx:Script>
 <CDATA[[
        protected function stpprHour_changeHandler(event:NumericStepperEvent):void {
            formatStepper(stpprHours);              
        }

        protected function stpprMinutes_changeHandler(event:NumericStepperEvent):void {
            formatStepper(stpprMinute);                             
        }

        protected function formatStepper(stepper : NumericStepper) : void {
            if(stepper.value<10) stepper.mx_internal::inputField.text = "0" + stepper.value;

    }
 ]]>
</mx:Script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文