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

  • Home
  • SEARCH
  • 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 7884667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:51:43+00:00 2026-06-03T04:51:43+00:00

I have code that takes as an argument a Dictionary and returns a Dictionary.

  • 0

I have code that takes as an argument a Dictionary and returns a Dictionary.

The code computes the sum of all the double values and uses the sum to compute what percentage each value is of the sum. It returns a new Dictionary with the percentages concatenated onto the keys.

There are some OverflowExceptions recorded in my web server’s Event Viewer. The log records that the exception occurs on the following code Decimal percentage = (Decimal) (pDataPoints[key] / sum * 100); It says that the exception occurs when casting the value to a decimal.

What edge case could I be missing?

public static Dictionary<string, double> addPercentagesToDataPointLabels(Dictionary<string, double> pDataPoints)
{
Dictionary<string, double> valuesToReturn = new Dictionary<string, double>();

// First, compute the sum of the data point values
double sum = 0;
foreach (double d in pDataPoints.Values)
{
    sum += d;
}

// Now, compute the percentages using the sum and add them to the new labels.
foreach (string key in pDataPoints.Keys)
{
    string newKey = key;
    Decimal percentage = (Decimal) (pDataPoints[key] / sum * 100);
    percentage = Math.Round(percentage, ChartingValues.DIGITS_AFTER_DECIMAL_POINT);
    newKey += " " + percentage.ToString() + "%";
    valuesToReturn.Add(newKey, pDataPoints[key]);
}

return valuesToReturn;
}
  • 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-03T04:51:44+00:00Added an answer on June 3, 2026 at 4:51 am

    Here you go:

    addPercentagesToDataPointLabels(new Dictionary<string, double>(){{"a", 0}});
    

    Like gdoron said, you divide by 0. But it throws an exception only for ints. For floating point numbers, it results in Double.Infinity. It then tries to convert infinity to decimal.

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

Sidebar

Related Questions

I have the following code that takes a double value and converts it to
I have a dictionary that I want to add all the values to an
I have a piece of code that takes several rows from a database and
I have a code at the moment that takes a line from a .txt
I have this small piece of code that basically takes a list and runs
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have a function that takes an ostream reference as an argument, writes some
I have a CommandArgument that takes in 1 argument from a LinkButton in my
I have to write a program that takes a command-line argument n and prints
I have an application that takes a dictionary of files (file type, and list

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.