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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:33:48+00:00 2026-06-15T23:33:48+00:00

Similar to this question just in C# instead of JavaScript. I couldn’t find anything

  • 0

Similar to this question just in C# instead of JavaScript. I couldn’t find anything for C# while searching

I have a text box that will take a quantity, which is later stored as a double in a database. However it is possible that some quantities will be entered as string fractions e.g 1/2 for 0.5.
I want to be able to convert these to decimal before storing them to the database (being able to also convert back would be nice but not necessary). I want to be able to handle both fractions and mixed numbers e.g. 2 1/2 is saved as 2.5

Anybody know a way of doing this?

  • 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-15T23:33:50+00:00Added an answer on June 15, 2026 at 11:33 pm

    Try splitting it on the space and slash, something like:

    double FractionToDouble(string fraction) {
        double result;
    
        if(double.TryParse(fraction, out result)) {
            return result;
        }
    
        string[] split = fraction.Split(new char[] { ' ', '/' });
    
        if(split.Length == 2 || split.Length == 3) {
            int a, b;
    
            if(int.TryParse(split[0], out a) && int.TryParse(split[1], out b)) {
                if(split.Length == 2) {
                    return (double)a / b;
                }
    
                int c;
    
                if(int.TryParse(split[2], out c)) {
                    return a + (double)b / c;
                }
            }
        }
    
        throw new FormatException("Not a valid fraction.");
    }
    

    Hey, it worked! Remember to check for a division by zero, too. You’ll get Infinity, -Infinity, or NaN as a result.

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

Sidebar

Related Questions

Similar to this question: link However I have already mastered that. My problem is
Similar to this question I have a custom subclass of UITableViewCell that has a
I swear I have poured and poured over every other similar question on this
I have a VERY similar question to this question: ASP.NET MVC C# dynamic radiobuttons
I know similar permutations of this question have been asked before, but the answers
I apologize in advance for asking this question, I know similar questions have already
I have a similar question to this one: Merge the contents of 2 cells
I have a similar question to this one SQL products/productsales I want to do
Similar to this question I am using a custom VirtualPathProvider to retrieve views that
Similar to this question asking about relatively light-weight data entry / editor application development,

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.