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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:28:35+00:00 2026-06-10T16:28:35+00:00

I currently got the following method, which is returning me percent-values. For example for

  • 0

I currently got the following method, which is returning me percent-values. For example for an item-price of $350,000 and a percentage of 7%, it returns 24,500.

    public static decimal GetPercentValue(decimal? percentage, decimal baseValue)
    {
        decimal result = 0m;

        if (percentage != null)
        {
            try
            {
                result = Convert.ToDecimal(baseValue * percentage / 100m);
            }
            catch (OverflowException)
            {
                result = 0;
                Logger.Warn("OverflowException caught in GetPercentValue() - should better be handled UI-Sided!");
            }
        }

        return result;
    }

I don’t think this is handled the right way, so is there any way to avoid an exception in this situation?

An OverflowException is being thrown when a user enters an insane number like 999,999,999,999,999,999 and calculates 9999999999% of it. This way I can’t check percentage or baseValue for <= decimal.MaxValue simply because they aren’t… The calculation result itself then exceeds the decimal range.

  • 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-10T16:28:37+00:00Added an answer on June 10, 2026 at 4:28 pm

    The error handling should (most likely) be done outside the method. Right now you’re hiding exceptions and returning wrong results (0 is returned when an error occures). The caller of your method cannot tell if the result is correct or if it’s due to an OverflowException.

    I’d rewrite the method like that:

    public static decimal GetPercentValue(decimal? percentage, decimal baseValue)
    {
        if (percentage == null)
            return 0;
    
        return baseValue*(percentage.Value/100);
    }
    

    And optionally add a validation method that the user can call to check the parameters before calling the real method.. validation errors could be displayed in the UI:

    public static string ValidatePercentValue(decimal? percentage, decimal baseValue)
    {
        try
        {
            GetPercentValue(percentage, baseValue);
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    

    Besides that note that…

    baseValue*(percentage.Value/100)
    

    … is better than…

    baseValue*percentage.Value/100
    

    Try to calculate 100% of decimal.MaxValue. The first one works while the second one throws an OverflowException.

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

Sidebar

Related Questions

I've got a method which currently takes an IList as a parameter, where UserBO
I have the following method which returns true when a PropertyInfo instance references an
I successfully got the application icons of the currently installed applications with the following
Using the following code, we got the error 'Object is currently in use elsewhere'
I'm currently preparing a major Program-Update and ran into following problem: I've got a
I've currently got a webserver set up which I communicate over SOAP with my
I've currently got a Bash command being executed (via Python's subprocess.Popen ) which is
I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()
Hi all i've got the following code which will go off and get all
So, I've got a multithreaded python program, which is currently suffering from deadlock. I

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.