JS中的数学 - 如何从百分比中获得比率

问题描述 投票:1回答:3

我正在尝试制作一个转换器,但我不知道这样做的公式,例如,我如何得到30711152的85694的比率。所以,我可以得到像85694/30711152 * 100 = 0.28的% (四舍五入)但是如何在100中获得类似1的比例?我相信大概是1:400左右?但我不知道如何准确地使用它或使用什么配方......

javascript math rounding fractions
3个回答
5
投票

30711152 / 85694的比例为1。只需反转分数。


1
投票

那么,这个比例保持不变。如果您的比例为3:12,则相当于1:4的比例,而这相当于25%。 所以85694:30711152 = 1:358.381。


1
投票

我意识到这很古老,但最近我遇到了这个问题。我需要给出关系给定人口的两个部分,其中数字可能非常大,但需要简化的比例,如3:5或2:7。我想出了这个,希望它有用:

function getRatio(a,b,tolerance) { 

/*where a is the first number, b is the second number,  and tolerance is a percentage 
of allowable error expressed as a decimal. 753,4466,.08 = 1:6, 753,4466,.05 = 14:83,*/

    if (a > b) { var bg = a; var sm = b; } else { var bg = b; var sm = a; }
    for (var i = 1; i < 1000000; i++) {
        var d = sm / i;
        var res = bg / d;
        var howClose = Math.abs(res - res.toFixed(0));
        if (howClose < tolerance) {
            if (a > b) { 
               return res.toFixed(0) + ':' + i; 
            } else { 
               return i + ':' + res.toFixed(0); 
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.