## 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");
}
```

1. Thanks for your post. seems to be useful for me.
دانلود فیلم خارجی

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.

more details click the link now

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

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

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

5. Hiring a bodyguard in the UK is not as difficult as it seems. There are several companies that can provide bodyguard services in the country. bodyguard company
People need services for various reasons. Some need them for their security in an event, high-profile individuals need bodyguards at the fear of kidnapping.

6. How to Play Baccarat in India - WLRione
Learn worrione how to play Baccarat in India and how to get started 1xbet korean with This page offers information about how to play 메리트카지노 Baccarat in India.

7. I cant taking office on focusing long adequate to research; lots less write this rosy of article. Youve outdone your self as quickly as this cloth truely. it's miles one of the greatest contents. Game Maker Studio 2 Crack

8. I go to your blog frequently and counsel it to the complete of folks who desired to feature-on happening their understanding subsequent to ease. The style of writing is exquisite and plus the content material is summit-notch. thanks for that perception you provide the readers! AVG Driver Updater Keys

9. i'm incapable of reading articles online particularly frequently, however Im happy I did nowadays. it is selected adroitly written, and your points are adeptly-expressed. I demand you harmoniously, entertain, dont ever lower writing. Memory Birthday Wishes For EX BoyFriend