-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make bigRat("0.35%") work #22
Comments
Easy way to do this is here... var str = "354.25%";
if(str.indexOf("%") !== -1 ){
var n = bigRat(str.substr(0, str.indexOf("%"))).divide(bigRat('100'));
}
//output
document.write(n.toString(), '==', n.toDecimal()); I did found in the source code the parse function with name "function parse(a, b) {...}" if(text.indexOf("%")!==-1){return parseDecimal(text.substr(0, text.indexOf("%"))).divide(bigRat('100'));}
return parseDecimal(text); test: var str = "354.25%";
var bigrat = bigRat(str);
console.log(bigrat.toString(), '=', bigrat.toDecimal()); But for example, bigRat not working correctly with var string = "2_1/3%" and need do more changes. So I did rewrite BigRational JS to add supporting persents. function parse(a, b) {
if(!a) {
return new BigRational(bigInt(0), bigInt[1]);
}
if(b) {
return reduce(bigInt(a), bigInt(b));
}
if (bigInt.isInstance(a)) {
return new BigRational(a, bigInt[1]);
}
if (a instanceof BigRational) return a;
var num;
var denom;
var text = String(a);
var persents = text.split("%");
if(persents.length>2){
throw new Error("Invalid input: too many '%' tokens");
}
if(persents.length>1){
text = persents[0];
var persent = true;
}
var texts = text.split("/");
if(texts.length > 2) {
throw new Error("Invalid input: too many '/' tokens");
}
if(texts.length > 1) {
var parts = texts[0].split("_");
if(parts.length > 2) {
throw new Error("Invalid input: too many '_' tokens");
}
if(parts.length > 1) {
var isPositive = parts[0][0] !== "-";
num = bigInt(parts[0]).times(texts[1]);
if(isPositive) {
num = num.add(parts[1]);
} else {
num = num.subtract(parts[1]);
}
denom = bigInt(texts[1]);
return (persent) ? reduce(num, denom).divide(bigRat('100')) : reduce(num, denom);
}
return (persent) ? reduce(bigInt(texts[0]), bigInt(texts[1])).divide(bigRat('100')) : reduce(bigInt(texts[0]), bigInt(texts[1]));
}
return (persent) ? parseDecimal(text).divide(bigRat('100')) : parseDecimal(text);
} test: <script language="JavaScript" type="text/javascript" src="../BigInteger.js"></script>
<!-- This need to require bigInteger previous ↑ -->
<script language="JavaScript" type="text/javascript" src="BigRational.js"></script>
<script>
var str = "354.25%";
var bigrat = bigRat(str);
document.write('<br>', str, ' = ', bigrat.toString(), ' = ', bigrat.toDecimal());
var str = "2_1/3%";
var bigrat = bigRat(str);
document.write('<br>', str, ' = ', bigrat.toString(), ' = ', bigrat.toDecimal());
var str = "1/3%";
var bigrat = bigRat(str);
document.write('<br>', str, ' = ', bigrat.toString(), ' = ', bigrat.toDecimal());
var str = "0.00003%";
var bigrat = bigRat(str);
document.write('<br>', str, ' = ', bigrat.toString(), ' = ', bigrat.toDecimal());
</script> result
|
Add % to parse this from string. See: peterolson#22 Warning! Strings, like "135.16531%1.16/51" will be parsed as 135.16531/100. Only number, before symbol %. Strings, like "2_1/8%" working and will be parsed as 2.125/100 = 0.02125
Add tests for issues peterolson#22, peterolson#26 after some fixes
Happy to have a go at this myself presently.
The text was updated successfully, but these errors were encountered: