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

Getting 2 decimal places

Participant ,
Mar 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

Hi all,

I'm trying to get one of my maths equations to be rounded to 2 decimal places, but when I do this, it rounds it too much.

The code I've been experimenting with:

var decimalPlace1: Number = Math.round(mLvl1Qu5RanNu1 * 10) / 100;

trace("decimalPlace1", decimalPlace1);

var decimalPlace2: Number = Math.round(mLvl1Qu5RanNu2 * 10) / 100;

trace("decimalPlace2", decimalPlace2);

var mLvl1Qu5Equal = decimalPlace1 - decimalPlace2;

trace("equals", mLvl1Qu5Equal);

var mLvl1Qu5EqualDecimal = Math.round(mLvl1Qu5Equal * 10) / 100;

trace("mLvl1Qu5EqualDecimal", mLvl1Qu5EqualDecimal);

The output

decimalPlace1 50.99

decimalPlace2 35.32

equals 15.670000000000002

mLvl1Qu5EqualDecimal 1.57

I got it working without the code in bold, but for some reason it's stopped working again.

I need to check the answer of mLvl1Qu5Equal against what the user enters.

Thanks,

Jack

Views

4.9K

Translate

Translate

Report

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 , Mar 24, 2017 Mar 24, 2017

To get two decimal places you would usually do this:

trace(Math.round(num*100)/100);

Perhaps you typed it as 10 by mistake?

Just do the two decimal places at the end, because if you do a calculation using two 2 decimal place numbers, it can become a full floating point number again. So, your entire script could become:

var mLvl1Qu5Equal = Math.round((mLvl1Qu5RanNu1 - mLvl1Qu5RanNu2) * 100)/100);

Votes

Translate

Translate
LEGEND ,
Mar 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

To get two decimal places you would usually do this:

trace(Math.round(num*100)/100);

Perhaps you typed it as 10 by mistake?

Just do the two decimal places at the end, because if you do a calculation using two 2 decimal place numbers, it can become a full floating point number again. So, your entire script could become:

var mLvl1Qu5Equal = Math.round((mLvl1Qu5RanNu1 - mLvl1Qu5RanNu2) * 100)/100);

Votes

Translate

Translate

Report

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
Participant ,
Mar 25, 2017 Mar 25, 2017

Copy link to clipboard

Copied

LATEST

Thanks Colin Holgate​, worked a treat.

For future reference for anyone else, if they want to display the decimal point to the user 0's don't appear. to fix this, put .toFixed(2) on the end of the variable you want to display. This will force it to display 2 digits after the dot.

Thanks robdillon​ for the link to the guide

Thanks,
Jack

Votes

Translate

Translate

Report

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 ,
Mar 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

Here's the official Flash method for getting a given number of decimal places. Rounding to specific decimal places in Flash

Votes

Translate

Translate

Report

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 ,
Mar 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

One problem with the official method, which is almost identical to what we've been talking about, is that int() rounds down. It acts like Math.floor(), and Jack wants the nearest decimal place, hence Math.round().

Votes

Translate

Translate

Report

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