Skip to content
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

Open
ghost opened this issue Feb 29, 2016 · 1 comment
Open

make bigRat("0.35%") work #22

ghost opened this issue Feb 29, 2016 · 1 comment

Comments

@ghost
Copy link

ghost commented Feb 29, 2016

Happy to have a go at this myself presently.

@username1565
Copy link

Easy way to do this is here...
"0.35%" this is a string. So you can so this with this code:

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) {...}"
So you can put there, in the end - this code:

	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.
You can see the code here. BigRational.js:

    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

354.25% = 1417/400 = 3.5425
2_1/3% = 7/300 = 0.0233333333
1/3% = 1/300 = 0.0033333333
0.00003% = 3/10000000 = 0.0000003

username1565 added a commit to username1565/BigRational.js that referenced this issue May 29, 2018
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
username1565 added a commit to username1565/BigRational.js that referenced this issue Jun 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant