Reputation: 817
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.
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
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
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