最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Convert decimal amount to text string fraction in Javascript? - Stack Overflow

programmeradmin3浏览0评论

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.....

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.....
Share Improve this question asked Aug 30, 2011 at 20:12 SteveSteve 14.9k37 gold badges138 silver badges245 bronze badges 5
  • Why are using so mane else if statements. Why not a switch case? – Joseph Silber Commented Aug 30, 2011 at 20:16
  • switch case only accepts one value and implies equals (=). – Diodeus - James MacFarlane Commented Aug 30, 2011 at 20:18
  • @joseph how yu wil give switch case if value is like .22 .23 .25 .27 – zod Commented Aug 30, 2011 at 20:19
  • What isn't clear is, what happens when you reach a value greater than 1? Are you going to return things like "1 1/2" for 1.5? – user229044 Commented Aug 30, 2011 at 20:21
  • @Diodeus: That's true, but you can trick it into evaluting stuff for you: jsfiddle/9XWAA , although the main idea of a switch is the simplify matters, not plicate them ;) – Joseph Silber Commented Aug 30, 2011 at 20:28
Add a ment  | 

4 Answers 4

Reset to default 3

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
}

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";

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 plex, 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));
  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

发布评论

评论列表(0)

  1. 暂无评论