Answered by:
Division result is very small, always shows as 0
Question

User718146471 posted
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?
Wednesday, January 8, 2020 4:49 PM
Answers

User753101303 posted
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 4e7 Console.WriteLine(a); Console.WriteLine(b); } } }
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 8, 2020 5:56 PM 
User475983607 posted
Ok, that gave me the decimal result I was expecting. Now, with it being 0.0003 (Rounded to first nonzero 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; } }
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 8, 2020 9:12 PM 
User475983607 posted
Is there a way to show 5.55/100 instead of 5/100? Just so the appearance of the risk density appears closer.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/enus/dotnet/standard/basetypes/standardnumericformatstrings
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 9, 2020 1:52 PM
All replies

User753101303 posted
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 4e7 Console.WriteLine(a); Console.WriteLine(b); } } }
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 8, 2020 5:56 PM 
User718146471 posted
Ok, that gave me the decimal result I was expecting. Now, with it being 0.0003 (Rounded to first nonzero 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?
Wednesday, January 8, 2020 8:22 PM 
User475983607 posted
Ok, that gave me the decimal result I was expecting. Now, with it being 0.0003 (Rounded to first nonzero 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; } }
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 8, 2020 9:12 PM 
User718146471 posted
Thank you, that worked amazingly!
Thursday, January 9, 2020 2:12 AM 
User718146471 posted
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 = 2000Result = .0555
Is there a way to show 5.55/100 instead of 5/100? Just so the appearance of the risk density appears closer.
Thursday, January 9, 2020 1:17 PM 
User475983607 posted
Is there a way to show 5.55/100 instead of 5/100? Just so the appearance of the risk density appears closer.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/enus/dotnet/standard/basetypes/standardnumericformatstrings
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 9, 2020 1:52 PM