Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:20:15+00:00 2026-05-25T10:20:15+00:00

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

  • 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.....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:20:15+00:00Added an answer on May 25, 2026 at 10:20 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the value 0.0042 returned and I pass this to: string.Format({0:C},....); It
I have a perl variable $results that gets returned from a service. The value
I have string value and Type of some variable (For example int and 32
I have a Double which could have a value from around 0.000001 to 1,000,000,000.000
I have the following data returned buy a simple SQL query. The Number of
I have the following values being returned in a column.. 0.250000 and 13.000000. I'd
I have a value like this: Foo Bar Another Value something else What regex
Doesn't value have to return toString() to be able to call value.toString()? When do
I have a value __int64 that is a 64-bit FILETIME value. FILETIME has a
I have the value of the epoch time like say 123456789, now I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.