locked
Converting in C# from decimal to x of y RRS feed

  • Question

  • User-718146471 posted

    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!

    Monday, January 6, 2020 3:16 PM

Answers

  • User475983607 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2020 2:05 PM

All replies

  • User475983607 posted

    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

    Monday, January 6, 2020 3:23 PM
  • User-718146471 posted

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

    Monday, January 6, 2020 3:29 PM
  • User475983607 posted

    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. 

    Monday, January 6, 2020 3:36 PM
  • User-718146471 posted

    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.

    Wednesday, January 8, 2020 1:03 PM
  • User475983607 posted

    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?   

    Wednesday, January 8, 2020 1:36 PM
  • User-718146471 posted

    330/4090 = 0.008

    Actually, that would be 330/40900 = .0008

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

    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?

    Wednesday, January 8, 2020 1:50 PM
  • User475983607 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2020 2:05 PM
  • User475983607 posted

    I forgot to add the multiplier value can be found by...

    SELECT POWER(10, @tenths);

    Wednesday, January 8, 2020 3:12 PM