Comments (6)
I've added toRadixedString(radix, numFractionalDigits)
, toRadixedString(radix, numFractionalDigits, roundingMode)
, valueOf(String, radix)
, and BigFraction(String, radix)
. I left toDecimalString()
as-is: it just calls toRadixedString()
with radix defaulted to 10.
Let me know if you run into any issues using them and thanks again for the suggestions.
from bigfraction.
Done!
from bigfraction.
Nice!
I noticed toDecimalString() hasn't been implemented yet, though I imagine it's harder to do.
from bigfraction.
Actually, I figured out how to implement it.
//CHANGED: Added a radix argument
public String toDecimalString(int numDecimalDigits, int radix, RoundingMode roundingMode)
{
if(numDecimalDigits < 0)
throw new IllegalArgumentException("numDecimalDigits must be nonnegative");
//shortcut - if we don't want any decimal digits, this is equivalent to round()
if(numDecimalDigits == 0)
return this.round(roundingMode).toString();
//CHANGED: multiply by radix^(digits) instead of 10^(digits)
//multiply by 10^(digits), then round to integer
BigInteger rounded = this.multiply(BigInteger.valueOf(radix).pow(numDecimalDigits)).round(roundingMode);
//CHANGED: added radix argument to the toString method
//get the actual digits (ignoring the sign bit)
String digits = rounded.abs().toString(radix);
String beforeDecimal = "0";
String afterDecimal = digits;
int padLen = 0; //number of zeros we need to pad afterDecimal with with
if(digits.length() > numDecimalDigits)
{
//we got too many digits... need to split into before/after decimal parts
beforeDecimal = digits.substring(0, digits.length() - numDecimalDigits);
afterDecimal = digits.substring(digits.length() - numDecimalDigits);
}
else if (digits.length() < numDecimalDigits)
{
//we don't have enough digits. We will have to pad with zeros
padLen = numDecimalDigits - digits.length();
}
//else: we got exactly the right number of digits. nothing to do!
//create string builder to hold result. init buffer to max possible size: lenth of parts plus length of padding plus space for . and -
StringBuilder sb = new StringBuilder(beforeDecimal.length() + afterDecimal.length() + padLen + 2);
if(rounded.signum() < 0)
sb.append('-');
sb.append(beforeDecimal).append('.');
for(int i = 0; i < padLen; i++)
sb.append('0');
sb.append(afterDecimal);
return sb.toString();
}
from bigfraction.
I will look at toDecimalString() later.
The existing name of the function is unfortunate, since "decimal" means base 10, and choosing a different radix would no longer make it a decimal representation. I will probably keep the function name the same for backwards compatibility, and also because as far as I can tell there is no good generalized name for non-fractional representation of a number in an arbitrary base. Here's someone asking http://math.stackexchange.com/questions/93315/name-for-decimals-in-other-bases
from bigfraction.
imo "toRadixedString()" is a perfectly fine method name, and my modifications shouldn't make a difference to performance.
Don't forget, though, that the different radices also have to be supported by the String constructor and valueOf method. Since the valueOf(String) method currently make use of BigDecimal, they'll probably have to be rewritten.
from bigfraction.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bigfraction.