Tony_89
Tony_89

Reputation: 817

Get Percentage of Width

I have been looking through answers on here and i know this is similar to other questions but what i have tried gives me the incorrect result. I am drawing a graph in c#. I have a width as seen below.

public const int IMAGE_HEIGHT = 1920;    //canvas heigh
public const int IMAGE_WIDTH = 1080;     //canvas width
public const int BOREHOLE_RECT_WIDTH = (IMAGE_WIDTH / 2 - (10 * MARGIN));

The important value is BOREHOLE_RECT_WIDTH which returns as 240. I am trying to work out what 15 percent is of this value. I have checked this on the website below and others and 15 percent of 240 is 36.

Percentage Calculator

However when i use the code below i get a result of 6 which does not seem correct to me.

 int percent = (int)Math.Round((double)(100 * 15) / (double)BOREHOLE_RECT_WIDTH);

I am not sure what im doing wrong so any help would be greatly appreciated.

Upvotes: 0

Views: 669

Answers (2)

anthonytimmers
anthonytimmers

Reputation: 268

You are calculating what 1500 / 240 is currently, which equals to 6.25. If you want to get the answer you either have to do:

(240 ÷ 100) × 15

OR

240 * 0.15 

Upvotes: 0

MajkeloDev
MajkeloDev

Reputation: 1661

Te get 15 percent of somethin You just need to multiply it by 0.15. For instance

var percent = Value * 0.15; // if Value == 240 thant percent will be 36

Or You can write a method for it so You will be able to get any percentage You want for example:

public double PercentOf(double percent, double number)
{
    return number * (percent / 100);
}

Upvotes: 3

Related Questions