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?

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?

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;
}
}

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.

All-Star

35208 Points

9948 Posts

Moderator

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

Jan 08, 2020 04:49 PM|bbcompent1|LINK

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.All-Star

47030 Points

17320 Posts

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

Jan 08, 2020 05:56 PM|PatriceSc|LINK

Hi,

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

All-Star

35208 Points

9948 Posts

Moderator

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

Jan 08, 2020 08:22 PM|bbcompent1|LINK

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.All-Star

47331 Points

20737 Posts

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

Jan 08, 2020 09:12 PM|mgebhard|LINK

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.

All-Star

35208 Points

9948 Posts

Moderator

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

Jan 09, 2020 02:12 AM|bbcompent1|LINK

Thank you, that worked amazingly!

Mark as answer posts that helped you.All-Star

35208 Points

9948 Posts

Moderator

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

Jan 09, 2020 01:17 PM|bbcompent1|LINK

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.All-Star

47331 Points

20737 Posts

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

Jan 09, 2020 01:52 PM|mgebhard|LINK

Sure, just use standard C# numeric formats when converting from a numeric type to a string.

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

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings