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 8807991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:30:24+00:00 2026-06-14T02:30:24+00:00

How do I convert 1 1/4 to 1.25? I want to take user input

  • 0

How do I convert “1 1/4” to 1.25?

I want to take user input and convert it to the logical numerical equivalent. I say logical because 2 R and R 2 need to be 2(has to do with measuring chiropractic adjustments). Everything worked until they needed to be able to use mixed fractions.

Is there a library for this?

The only number that does not work is “1 1/4” which is converted in error to “2.75”.

    // Sample input
    var values = ["2.5", "1 1/4", "1/4", "2 R", "R 2"];

    function l(msg) {
        console.log(msg)
    }

    function toDecimal(x) {
        if (x.indexOf('/') != -1) {
            var parts = x.split(" ")
            var decParts = parts[1].split("/")
            return parseInt(parts[0], 10) + (parseInt(decParts[0], 10) / parseInt(decParts[1], 10))
        } else {
            return x
        }
    }

    function total_it_up(values){
        var total = 0,
            value = 0

        if(values === undefined)
            return 0

        $.each(values, function(index, value){
            value = value.replace(/[^0-9./]+/g, "")
            value = eval(value)
            l(value)
            total += parseFloat(value)
        })

        return total
    }

SOLUTION

    function toDecimal(x) {
        if (x.indexOf('/') != -1) {
            var parts = x.split(" ")
            var decParts;
            if (parts.length > 1) {
                decParts = parts[1].split("/");
            }
            else {
                decParts = parts[0].split("/");
                parts[0] = 0;
            }
            return parseInt(parts[0], 10) + (parseInt(decParts[0], 10) / parseInt(decParts[1], 10))
        } else {
            return x
        }
    }


    function total_it_up(values){
        var total = 0;

        if(values === undefined)
            return 0;

        $.each(values, function(index, value){
            total += parseFloat(toDecimal($.trim(value.replace(/[^0-9./ ]+/g, ""))));
        })

        return total;
    }
  • 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-06-14T02:30:26+00:00Added an answer on June 14, 2026 at 2:30 am

    We don’t see any call for toDecimal(), so I’m assuming the $.each loop is calling it in your original code. If that is the case, then 1 1/4 is coming up as 2.75 because the regular expression is stripping the space between 1 and 1/4, resulting in 11/4, which is 2.75. Allow spaces, but $.trim the result so that any leading or trailing spaces are removed. You also shouldn’t have to use eval().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently we are working with a user input of time. We want to take
I am trying to take user input and convert it in the form of
I want to take an expression in tcl and convert its _ to <
I want to take an ExtJS element(div) retrieved from the DOM selector, and convert
I want to convert plaintext to link for an input I have. The HTML
I'm creating a user input where I want them to list links as well
So, for an assignment I would have to take user input such as 1146
I'm using Datjs to take a date from a user and convert it to
i have a requirement, i want to take URL of image from user and
I want to convert a string to formatted text in C# in WPF application,

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.