 ## 8 replies

Last post Jan 08, 2020 03:12 PM by mgebhard

• ### Converting in C# from decimal to x of y

Hello folks, I have one here that is really tickling my brain and while it sounds like it should be quite simple, it is becoming increasingly difficult. Here's the skinny. To figure out my vulnerability risk density, I have to take vulnerabilities and divide that by lines of code. For a simplified example,

500 (vulnerabilities) / 50000 (LOC) = .01 Risk Density or 1%

What I want to do is show this as X vulnerabilities for every Y lines of code (1,000) instead of showing it as a percentage or decimal. Any ideas? Thanks all!

Mark as answer posts that helped you. • ### Re: Converting in C# from decimal to x of y

#### bbcompent1

Hello folks, I have one here that is really tickling my brain and while it sounds like it should be quite simple, it is becoming increasingly difficult. Here's the skinny. To figure out my vulnerability risk density, I have to take vulnerabilities and divide that by lines of code. For a simplified example,

500 (vulnerabilities) / 50000 (LOC) = .01 Risk Density or 1%

What I want to do is show this as X vulnerabilities for every Y lines of code (1,000) instead of showing it as a percentage or decimal. Any ideas? Thanks all!

1% can be written as 10/1000.

Or if you want to go from 500/50,000 to X/1,000 then divide by 50. ie. 50,000 / 50 = 1000 and 500 / 50 = 10.  To find 50, the equation is 50,000  = 1000 * X.

Or

X= 50,000 / 1,000

• ### Re: Converting in C# from decimal to x of y

Ok, I'll play around with that and try generating that as a code-based solution. Thank you.

Mark as answer posts that helped you. • ### Re: Converting in C# from decimal to x of y

#### bbcompent1

Ok, I'll play around with that and try generating that as a code-based solution. Thank you.

The easiest solution is multiplying the decimal value by 1000.  (0.01 * 1,000) = 10 / 1000

Be mindful of significant figures.

• ### Re: Converting in C# from decimal to x of y

Ok, that makes sense. In the c# code behind, is there some way to determine what that multiplier value would be? Here's a real example of what I'm trying to show:

 Project Issue Count Lines of Code Risk Density (RD) (Vuln/LOC) Math Calculation (Issues/LOC = RD * Multiplier) App1 867 2795641 3/10000 867/2795641 = .0003 * 10000 = 3 / 10000 App2 603 1411731 3/1000 603/1411731 = .0004 * 10000 = 4 / 10000 App3 330 40900 4/100 330/40900 = .004 * 1000 = 8 / 1000 App4 299 48103 2/100 299/48103 = .006 * 1000 = 6 / 1000 App5 212 242261 6/1000 212/242261 = .0008 * 10000 = 8 / 10000

How would one go about determining the multiplier in code? The result of the RD is easy enough, the tricky part from my perspective is determining what that second value would be.

Mark as answer posts that helped you. • ### Re: Converting in C# from decimal to x of y

#### bbcompent1

How would one go about determining the multiplier in code? The result of the RD is easy enough, the tricky part from my perspective is determining what that second value would be.

Your math is incorrect.  I'm not sure if that's causing issues.

```330/4090 = 0.008
0.008 * 1000 = 8/1000 = 4/500 = 2/250```

You are trying to figure out where the decimal place is located?

• ### Re: Converting in C# from decimal to x of y

#### mgebhard

`330/4090 = 0.008`

Actually, that would be 330/40900 = .0008

.0008 * 10000 = 8/10000 = 4/5000 = 1/2500

#### mgebhard

You are trying to figure out where the decimal place is located?

Yes, so if my result is .0008 from division, I need to determine the multiplier value, in this case, .0008 needs to be multiplied by 10,000. In other cases, if my result is .02 then multiply by 100 for example. How do I do that?

Mark as answer posts that helped you. • ### Re: Converting in C# from decimal to x of y

#### bbcompent1

Yes, so if my result is .0008 from division, I need to determine the multiplier value, in this case, .0008 needs to be multiplied by 10,000. In other cases, if my result is .02 then multiply by 100 for example. How do I do that?

A loop, a counter, and a condition.

```DECLARE @v DECIMAL(7,6)
DECLARE @tenths INT = 0
SET @v = 0.0008

WHILE (@v < 1)
BEGIN
SET @tenths = @tenths + 1
SET @v = @V * 10
END

SELECT @tenths```

• ### Re: Converting in C# from decimal to x of y

`SELECT POWER(10, @tenths);`