Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:46:55+00:00 2026-06-15T10:46:55+00:00

while building an accounting system, I came across a major problem. Let’s say I

  • 0

while building an accounting system, I came across a major problem. Let’s say I do an invoice and then, I have to apply a credit with the same amount as a negative value, I sometimes have a rounding problem of 1¢..

My problem is regarding negative values only!

Let’s have a look at this script that is testing a specific positive and negative value. For debugging purpose, I also send the parameter expectedResult ;

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler()">

    <fx:Script>
        <![CDATA[
            import mx.formatters.CurrencyFormatter;
            import mx.formatters.NumberBaseRoundType;

            private function roundDecimal(num:Number, expectedResult:Number):void
            {
                var rnd:CurrencyFormatter = new CurrencyFormatter();
                rnd.precision = 2;
                rnd.useThousandsSeparator = false;
                rnd.currencySymbol = '';

                if (num >= 0)
                    rnd.rounding = NumberBaseRoundType.NEAREST;
                else
                    rnd.rounding = NumberBaseRoundType.DOWN; // The problem is here, ALWAYS!

                var result:Number = Number(rnd.format(num));

                if (result != expectedResult)
                    trace("Error : " + result + " (Expecting " + expectedResult + ")");
                else
                    trace("OK : " + result);
            }

            protected function creationCompleteHandler():void
            {
                var num:Number;
                var expectedResult:Number;

                // Test number 1
                num = 5.653;
                expectedResult = 5.65;
                roundDecimal(num, expectedResult);
                roundDecimal(-(num), -(expectedResult));

                trace("------------");

                // Test number 2
                num = 5.655;
                expectedResult = 5.66;
                roundDecimal(num, expectedResult);
                roundDecimal(-(num), -(expectedResult));

                trace("------------");

                // Test number 3
                num = 5.657;
                expectedResult = 5.66;
                roundDecimal(num, expectedResult);
                roundDecimal(-(num), -(expectedResult));

            }
        ]]>
    </fx:Script>

</s:WindowedApplication>

Of course I tried to modify the rounding type for negative values, without success..

Have a look at the different rounding types and their results :

//////////// Test 1 ////////////
if (num >= 0)
    rnd.rounding = NumberBaseRoundType.NEAREST;
else
    rnd.rounding = NumberBaseRoundType.DOWN; // The problem is here, ALWAYS!

OK : 5.65
Error : -5.66 (Expecting -5.65)
------------
OK : 5.66
OK : -5.66
------------
OK : 5.66
OK : -5.66

//////////// Test 2 ////////////
if (num >= 0)
    rnd.rounding = NumberBaseRoundType.NEAREST;
else
    rnd.rounding = NumberBaseRoundType.UP;

OK : 5.65
OK : -5.65
------------
OK : 5.66
Error : -5.65 (Expecting -5.66)
------------
OK : 5.66
Error : -5.65 (Expecting -5.66)

//////////// Test 3 ////////////
if (num >= 0)
    rnd.rounding = NumberBaseRoundType.NEAREST;
else
    rnd.rounding = NumberBaseRoundType.NEAREST; // The problem is here, ALWAYS!

OK : 5.65
OK : -5.65
------------
OK : 5.66
Error : -5.65 (Expecting -5.66)
------------
OK : 5.66
OK : -5.66

I’m really stuck here and would appreciate some help.. Thanks a lot!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:46:56+00:00Added an answer on June 15, 2026 at 10:46 am

    Check to see if it’s positive or negative and assign a sign to a var (1 or -1), take the absolute value ( Math.abs(value)) and round that. Then multiply it by the sign to get the result.

    That way you always get the same result and the whether it is positive or negative doesn’t matter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across with these situation while building a class diagram for a system,
While building several different projects in QtCreator, I have run across the following build
I have a weird problem while building VS2010 solution incl. unit tests on a
I came in touch with a little problem while building a model with a
While building a billing system I encountered the following problem: In scheduler.rake : @users.each
i have a problem while building and running an iphone application.Im using xml parsing
I seem to have hit a snag while building my project using Ant. The
I have done simple java app for blackberry, while building am getting following error.
I have been building a game for a while (nearly done) - But the
I was doing my academic project and while building and testing i have put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.