Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can't figure out how to solve the decimal point bug in this calculator code

Explorer ,
Apr 08, 2013 Apr 08, 2013

Hi guys, I'm new in flash and is currently learning how to build a simple calculator with multipliers (plus,minus,multiple,divide,decimal point and change sign) but I'm stuck on the decimal point and change sign.

var multiplier_old:Number = 10;

var multiplier_new:Number = 1;

// .: Sets the multipliers so that new input numbers become decimals of a lower unit column

action_point.addEventListener(MouseEvent.MOUSE_DOWN, function():void {

 

          multiplier_old = 1;

          multiplier_new = 0.1;

          point = true;

 

});

// Takes intput from the input_ buttons and adds it to the input after applying the multipliers.

// If `point` is true then the multiplier_new is divided by 10, also as described.

function inputNumber(n:Number):void {

 

          input = input * multiplier_old + n * multiplier_new;

 

                    if (point) {

                                    multiplier_new *= 0.1;

                   trace(multiplier_new);

                    }

 

          output_txt.text = input.toString();

}

Decimal point

The problem is that when I input 2.7 in the calculator, it will display the values in output_txt correctly. But then when I input 2.78, it will display 2.780000000000000000000000002. This will also happen to other numbers if the input is too big.

What I want is just 2.78. How do I change the codings to make 2.780000000000000000000000002 become 2.78?

Change sign

Any tips on how to start on this one?

Thanks for your time,

Zainu

TOPICS
ActionScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 08, 2013 Apr 08, 2013

You need to use rounding in order to keep the numbers down to limited decimal values.  To display specific decimal values use the toFixed() method of the String class when writing the text to the textfield.

To change the sign, multiply by -1.

Translate
LEGEND ,
Apr 08, 2013 Apr 08, 2013

You need to use rounding in order to keep the numbers down to limited decimal values.  To display specific decimal values use the toFixed() method of the String class when writing the text to the textfield.

To change the sign, multiply by -1.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2013 Apr 08, 2013

Hi Ned Murphy,

Thanks for replying. How do I know what number is require to put in input.toFixed(?) because there can be many different amount of digits involve in the output depending what numbers the guy press in the caculator number buttons.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2013 Apr 08, 2013

A calculator can allow you to specify how many decimal places to display, so what value you want in that argument is up to you to decide.  You can make it a user option or you can set it to a specific limit.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2013 Apr 08, 2013

I think you misunderstand what I mean. The weird decimal doesnt comes out after I press the 'equals' sign. It comes out when im just pressing on the caculator number buttons

First I click the no.2 button and then the decimal point button.

So the caculation output display will show

2.

after that I press the no.7 button

2.7

and then no.8 button. It appears like

2.7800000000000000002

This is the code I use when the user press the decimal point button.

// .: Sets the multipliers so that new input numbers become decimals of a lower unit column

action_point.addEventListener(MouseEvent.MOUSE_DOWN, function():void {

 

          multiplier_old = 1;

          multiplier_new = 0.1;

          point = true;

});

// Takes intput from the input_ buttons and adds it to the input after applying the multipliers in the method described above.

// If `point` is true then the multiplier_new is divided by 10, also as described.

function inputNumber(n:Number):void {

 

          input = input * multiplier_old + n * multiplier_new;

 

                    if (point) {

                             trace(multiplier_new.toFixed(3));

                              multiplier_new *= 0.1;

                              //trace(multiplier_new);

                    }

          output_txt.text = input.toString();

}

I think there is some code wrong in this function that makes this weird problem. I tried putting toFixed method but it's still not working.

Sorry for this long reply but I have to try my bestto explain with my bad english.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2013 Apr 08, 2013
LATEST

I understand the problem and identified the solution.  The problem has to do with the way computers handle numbers.  This is not a problem with Flash, but with computers in general.  There is not much you can do about that except to use rounding techniques to have the numbers display the way you intend them to appear.  Using the toFixed() method is one way to have the numbers appear the way you want.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines