Pages

Tuesday, April 24, 2018

How to convert a decimal to a fraction in X++ with Dynamics AX or Dynamics 365 FO

I had a customer who needed to convert a decimal to fraction, so I just translated this StackOverflow post where all credit is due. I'm sure someone else will find it useful at some point.

The two main things in the job below (details taken from StackOverflow) are "_real", which is the value you want to convert, and "_accuracy", which specifies the max relative error; not the max absolute error.

So _accuracy = 0.01 would find a fraction within 1% of the value.

I also quickly threw this job together, so I didn't test for extremely large integers or any edge cases.

Here's a simple job that demonstrates how it works.


static void KW_DecToFrac(Args _args)
{
    // Create a function that accepts these two parameters
    real                _real           = 0.45;
    real                _accuracy       = 0.01;
    
    int                 sign = _real < 0 ? -1 : (_real == 0 ? 0 : 1);
    real                maxError;
    System.Decimal      d;
    
    int n;
    int lower_n = 0;
    int lower_d = 1;
    int middle_n;
    int middle_d;
    int upper_n = 1;
    int upper_d = 1;
    int z;
    
    void f(int _N, int _D)
    {
        info(strFmt("%1/%2", _N, _D));
    }
    
    _real = abs(_real);
    
    maxError = sign == 0 ? _accuracy : _real * _accuracy;
    
    d = System.Math::Floor(_real);
    n = System.Decimal::ToInt32(d);
    
    _real -= n;
    
    if (_real < maxError)
    {
        f(sign * n, 1);
        return;
    }
    
    if (1 - maxError < _real)
    {
        f(sign * (n+1), 1);
        return;
    }
    
    while (true)
    {
        z++;
        middle_n = lower_n + upper_n;
        middle_d = lower_d + upper_d;
        
        if (middle_d * (_real + maxError) < middle_n)
        {
            upper_n = middle_n;
            upper_d = middle_d;
        }
        else if (middle_n < (_real - maxError) * middle_d)
        {
            lower_n = middle_n;
            lower_d = middle_d;
        }
        else          
        {
            f((n * middle_d + middle_n) * sign, middle_d);
            return;
        }        
    }
    
    info("Done");
}

20 comments:

  1. Thank for this blog more informative step by step and useful contents. I here by attached my site would you see this blog

    7 tips to start a career in digital marketing

    “Digital marketing is the marketing of product or service using digital technologies, mainly on the Internet, but also including mobile phones, display advertising, and any other digital medium”. This is the definition that you would get when you search for the term “Digital marketing” in google. Let’s give out a simpler explanation by saying, “the form of marketing, using the internet and technologies like phones, computer etc”.

    we have offered to the advanced syllabus course digital marketing for available join now

    more details click the link

    https://www.webdschool.com/digital-marketing-course-in-chennai.html

    ReplyDelete
  2. This blog amazing and useful information.

    Web designing trends in 2020

    When we look into the trends, everything which is ruling today’s world was once a start up and slowly begun getting into. But Now they have literally transformed our lives on a tremendous note. To name a few, Facebook, Whats App, Twitter can be a promising proof for such a transformation and have a true impact on the digital world.

    we have offered to the advanced syllabus course web design and development for available join now

    more details click the link now

    https://www.webdschool.com/web-development-course-in-chennai.html

    ReplyDelete
  3. Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  4. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. Hex To Decimal

    ReplyDelete
  5. Nice blog. You have provided such a useful information in this blog. Thanks for sharing. AWS Course in Chennai

    ReplyDelete
  6. Welcome to the beautiful world of Delhi escorts. Here you will get the most delicious and tempting Delhi escorts who are awaited so much to being physical with you. Yes, it is true just meet our Aerocity escorts girls, they are waiting to provide extraordinary service to you. Delhi Escorts is offering such beautiful and high-profile models available 24x7 hours for you.

    ReplyDelete
  7. เราคือผู้นำด้านเกมพนันออนไลน์ Major168 เราคือผู้ให้บริการ คาสิโนออนไลน์ ที่ได้รับรองว่าดีที่สุดในประเทศไทย มีค่ายเกมส์ให้เล่นมากมาย Sagaming, Sexy bacarat, Dreamgame, Ebet, Wm casino, Vivo gaming ไม่ผ่านเอเย่นต์ ระบบปลอดภัยมีทีมงานดูแลตลอด 24ชม.

    SAGAME88 แหล่งรวมเกมส์พนันออนไลน์ คาสิโนสด บาคาร่า กำถั่ว คาสิโนออนไลน์ ไฮโล รูเล็ต รับเครดิตฟรีเล่นได้ทุกเกมส์ โบนัสสมาชิกใหม่เพียบ พร้อมระบบฝากถอนออโต้ 10วิ เรามีทุกค่ายเกมส์ให้คุณเลือกเดิมพัน SA Game Sexy bacarat Dreamgame WM Casino VIVO Gaming Ebet เล่นได้ทุกเกมส์

    Our website ufabet provides betting services in the system of Auto Deposit-Withdrawal. Our members are not only in Thailand. Online football betting UEFA Bet market And this makes a guarantee that Ufabet168 is another reliable football betting website

    ufa through the website UFABET1688 a web gambling online , one that integrated all the bets from online casinos , online casino , online , and also have a game a lot more to be chosen to play such games.

    ReplyDelete
  8. Very nice post..After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic.
    This article inspired me to read more. keep it up.
    Reactjs Training in Chennai |
    Best Reactjs Training Institute in Chennai |
    Reactjs course in Chennai

    ReplyDelete