I have a very large Decimal number as a string, it cannot be accurately represented by a float or double, so I cannot use any function that works with numbers, I have to keep my number as string, and do all functions with strings.
For example: PI:
string pI = round("3.141592653589793239", 6);
will return "3.141593"
.
Precision normally refers to total number of digits, as such my answer has a Precision of 7, so I will call this Decimal Places as to not confuse this issue.
I realize I could just write a function to do this, and I am working on it, but I wanted to ask for the most accurate way of doing this, and so far that is not my code, I blame it on my headache, but I have spent way too much time looking for a solution, getting caught up in using this Library and that one, when it seems there should be a library that does just this, without using floating-point math, and I have looked at a few arbitrary-precision arithmetic libraries, and have not been able to figure out how to set the number of Decimal Places, and have tried using the Precision, and came to stand still, because the numbers it gave me were not correct at the low range, between 0-42 decimal places.
I am using this in a Qt Qml Felgo App, so I will be calling it from Qml JavaScript, via a C++ back end call.
Update: Writing my string round function got ugly real quick, too many things to check for, its just as easy as it may sound till you start testing from them.
Looking at
CLN: https://ginac.de/CLN/cln.html
MPIR: https://github.com/wbhart/mpir
A fork of GMP: http://gmplib.org/
TTMath BigNum: https://sourceforge.net/projects/ttmath/
And I have an issue with Boost that ended in a bug report, and I thought I could write my own round function, so I could use their library, but what is the point, no matter what Library I use, I have a requirement for accuracy, and decimal point places.
People that have a need for Math, need a Math Library that can handle all their needs, and rounding to a specific decimal place is a given, yet I am having a hard time finding a function in any library that can to this well, so I have to test every library, and that is not easy, first you have to learn how to use it, and if it does not work the way you think it should, you end up spending more time, like I am doing here, trying to figure it out, and trust me, I read all the post about using big numbers, Floats specifically, that I could find, but none of them really dealt with arbitrary decimal point precision, meaning to do math at a specific decimal precision, most people think more is more accurate, that is not my case, I am looking for a way to do math that will return the correct number and not an estimation of it, float and long double is an estimate I cannot use, it was off by 100 miles, that bugs me, but not as much as just bad estimates, boost gave me the correct number, I just could not figure out how to use precision to adjust the number of digits, and that is not really boost thing, since they have never though it important enough to add a function to set decimal places, few people I know care about precision, they just care about how many decimal places it has, and specifically returning a set number of decimal places without having to use precision to do so, I thought that if I took the length of the left side, or whole number side, and add the precision to it, that it should work, I was wrong, and the results were too, so what I really need is a library that understands this.
I relize now that every calculator is actually an estimator at best.
I did find a library that worked with numbers, did not try it, but to be complete here is is:
https://www.codewars.com/kata/5324945e2ece5e1f32000370
I like the concept of libmpdec, so I will work on that now.
I ended up using https://github.com/MikeMcl/bignumber.js it works great in Qml, just import "bignumber.js" as BigNumberJs, and following the instructions.
Thanks for all the help.