 ## 6 replies

Last post Jan 09, 2020 01:52 PM by mgebhard

• ### Division result is very small, always shows as 0

Greetings all, I have a bit of a head scratcher and I'm hoping someone can resolve it. Say for sake of argument, I have a numerator/denominator like so:

2/5000000 = 0.0000004

The result I get back in C# is 0, despite using float, double, or decimal (it doesn't seem to matter).

What c# math function should I use to display this very small decimal number?

Mark as answer posts that helped you. • ### Re: Division result is very small, always shows as 0

Hi,

Append .0 to force the use of double rather than integer values currently causing the use of  an integer division. A quick demo:

```using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
double a = 2 / 5000000; // integer division, shows 0
double b = 2.0 / 5000000.0; // double division, shows 4e-7
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}```
• ### Re: Division result is very small, always shows as 0

Ok, that gave me the decimal result I was expecting. Now, with it being 0.0003 (Rounded to first non-zero value), I would like to present that as 3/10000. I've tried several c# fraction helper classes but I always get bizarre fractions that don't make much sense to me. Have any examples that would work out the fraction like this?

Mark as answer posts that helped you. • ### Re: Division result is very small, always shows as 0

#### bbcompent1

Ok, that gave me the decimal result I was expecting. Now, with it being 0.0003 (Rounded to first non-zero value), I would like to present that as 3/10000. I've tried several c# fraction helper classes but I always get bizarre fractions that don't make much sense to me. Have any examples that would work out the fraction like this?

Use the same approach as your other recent thread.  You'll want to use the decimal type and be very very careful with significant figures.

```class Program
{
static void Main(string[] args)
{
decimal num = 0.0003m;
int exponent = GetExponent(num);
decimal value = (decimal)Math.Pow(10, exponent);

Console.WriteLine(\$"{(int)(num * value)}/{value}");
}

public static int GetExponent(decimal num)
{
int count = 0;
while(num < 1)
{
count++;
num = num * 10;
}

return count;
}
}```

• ### Re: Division result is very small, always shows as 0

Thank you, that worked amazingly!

Mark as answer posts that helped you. • ### Re: Division result is very small, always shows as 0

Ok, just one more question related to this. I was wondering if there were some way to include two decimal places for the risk density in that code? So, for sake of argument:

VULNS = 111
LOC = 2000

Result = .0555

Is there a way to show 5.55/100 instead of 5/100? Just so the appearance of the risk density appears closer.

Mark as answer posts that helped you. • ### Re: Division result is very small, always shows as 0

`Console.WriteLine(\$"{(num * value).ToString("0.00")}/{value}");`