在 Javascript 中将小数金额转换为文本字符串分数?

发布于 2024-12-02 04:49:49 字数 720 浏览 1 评论 0原文

我有一个以数字形式返回的值,可以是十进制数,例如“1.15”。

但是,我需要将范围内的所有数字格式化为给定的分数。例如,所有大于 0 但小于 0.2 的数字我想返回“1/8”。

我已经开始将其作为一系列 if/else 语句来执行此操作,但我想知道是否有更聪明、更简洁的方法。

if (amt > 0 && amt <= .2){
    q = '1/8';
} else if (amt > .2 && amt <= .3){
    q = '1/4';
}  else if (amt > .3 && amt <= .4){
    q = '1/3';
}  else if (amt > .4 && amt <= .5){
    q = '1/2';
} else if (amt > .5 && amt <= .7){
    q = '2/3';
} else if (amt > .7 && amt <= .8){
    q = '3/4';
} else if (amt > .8 && amt <= 1.0){
    q = '7/8';
} else if (amt > 1 && amt <= 1.1){
    q = '1';
} etc.....

I have a value returned as a number, with could be decimal number e.g. "1.15".

However, I need to format all numbers in a range to a given fraction. For example, all numbers greater than 0 but less than .2 I want to return "1/8".

I already started to do this as a series of if/else statements, but I was wondering if there was a smarter and neater way.

if (amt > 0 && amt <= .2){
    q = '1/8';
} else if (amt > .2 && amt <= .3){
    q = '1/4';
}  else if (amt > .3 && amt <= .4){
    q = '1/3';
}  else if (amt > .4 && amt <= .5){
    q = '1/2';
} else if (amt > .5 && amt <= .7){
    q = '2/3';
} else if (amt > .7 && amt <= .8){
    q = '3/4';
} else if (amt > .8 && amt <= 1.0){
    q = '7/8';
} else if (amt > 1 && amt <= 1.1){
    q = '1';
} etc.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃梦幻 2024-12-09 04:49:49

将代码移至函数中,然后您可以从正确的分支返回并跳过所有else语句,这会显着地清理事情:

function toFraction(amt) {
  if (amt > 0 && amt <= .2) return '1/8';
  if (amt <= .3) return '1/4';
  if (amt <= .4) return '1/3';
  if (amt <= .5) return '1/2';
  // etc
}

Move your code into a function, and you can return from the correct branch and skip all the else statements, which cleans things up pretty dramatically:

function toFraction(amt) {
  if (amt > 0 && amt <= .2) return '1/8';
  if (amt <= .3) return '1/4';
  if (amt <= .4) return '1/3';
  if (amt <= .5) return '1/2';
  // etc
}
云归处 2024-12-09 04:49:49

Ratio.js 有一个名为 .toQuantityOf() 的函数。

.toQuantityOf() 在根据提供的单位将分数的当前值转换为其近似值后返回一个新分数。如果将多个单位作为参数传递,则将返回与原始值最接近的匹配项。

.toString() 以字符串形式返回分数。

var fraction = Ratio.parse("1.15").toQuantityOf(2,3,4,8);
fraction.toString() == "9/8";

如果需要,.toLocaleString() 将返回混合数字。

fraction.toLocaleString() == "1 1/8";

Ratio.js has a function called .toQuantityOf().

.toQuantityOf() returns a new fraction after converting the current value of the fraction to it's approximate value based on the provided units. If multiple units are passed as arguments, then the closest match to the original value will be returned.

.toString() returns the fraction as a string.

var fraction = Ratio.parse("1.15").toQuantityOf(2,3,4,8);
fraction.toString() == "9/8";

.toLocaleString() will return mixed numbers if needed.

fraction.toLocaleString() == "1 1/8";
默嘫て 2024-12-09 04:49:49

您可以对 amt 执行数学运算,将其转换为某个范围内的整数值,然后将其用作数组查找的索引以获取分数文本。

如果您的范围相等,这可能很简单,但由于您有一些 1/10(0.3 到 0.4)和一些 2/10(0.5 到 0.7),所以它有点复杂,不仅仅是乘以 10。


fractional = {
    fractions: [ "1/8", "1/8", "1/4", "1/3", "1/2", "2/3", "2/3", "3/4", "7/8", "1" ],
    toFraction: function(amt) {
        return this.fractions[(""+(amt*10)).split('.')[0]];
    }
};

alert(fractional.toFraction(0.37));

You can perform mathematical operations on amt to transform it to an integer value within a range, then use that as an index for an array lookup to get the fraction text.

That can be simple if your ranges are equal, but since you have some 1/10th (0.3 to 0.4) and some 2/10ths (0.5 to 0.7) it's a little more complex, more than just multiply by 10.


fractional = {
    fractions: [ "1/8", "1/8", "1/4", "1/3", "1/2", "2/3", "2/3", "3/4", "7/8", "1" ],
    toFraction: function(amt) {
        return this.fractions[(""+(amt*10)).split('.')[0]];
    }
};

alert(fractional.toFraction(0.37));
看海 2024-12-09 04:49:49
  1. 获取整数部分
  2. amt 乘以 10 并从您之前构造的数组中

- 查找相应的字符串!注意 - 该数组是固定的 - 所以是静态的

  1. Multiple amt by 10 and get the integer part
  2. From an array that you have previous constructed - look up the corresponding string!

NB - That array is fixed - so is static

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文