How to format a number in percentage within JavaScript?

0

Gentlemen, I'm developing a billing monitoring dashboard. I want to turn this number 56% directly into my JavaScript code. Here is a snippet of my code, it is fed directly by a query from my SQL.

This is the query that feeds my field within JavaScript, the variable MT receives it.

<snk:query var="MT">

        SELECT
            A.REALIZADO,
            A.META,
            (A.REALIZADO/A.META)*100 AS ACUMULADO

            FROM (

            SELECT 
            ((SELECT ISNULL(SUM((ITE1.QTDNEG*ITE1.VLRUNIT)-ITE1.VLRDESC-ITE1.VLRREPRED),0)
             FROM SANKHYA.TGFCAB CAB1 (NOLOCK)
             LEFT JOIN SANKHYA.TGFITE   ITE1 (NOLOCK) ON ITE1.NUNOTA=CAB1.NUNOTA
             LEFT JOIN SANKHYA.TGFPRO   PRO1 (NOLOCK) ON ITE1.CODPROD = PRO1.CODPROD
             WHERE CAB1.TIPMOV = 'V'
             AND   MONTH(CAB1.DTFATUR)= DATEPART(MM,GETDATE())
             AND   YEAR (CAB1.DTFATUR)= DATEPART(YYYY,GETDATE())
             AND   ITE1.CODCFO IN (5102,5403,5405,5922,6102,6108,6110,6403,6404,6922)
             AND   CAB1.STATUSNFE <> 'D'
             AND   PRO1.AD_MARCA=14 ) -
            (SELECT CONVERT(DECIMAL(10,2),ISNULL(SUM((ITE1.QTDNEG*ITE1.VLRUNIT)-ITE1.VLRDESC-ITE1.VLRREPRED) ,0))
             FROM SANKHYA.TGFCAB CAB1 (NOLOCK)
             LEFT JOIN SANKHYA.TGFITE   ITE1 (NOLOCK) ON ITE1.NUNOTA=CAB1.NUNOTA 
             LEFT JOIN SANKHYA.TGFPRO   PRO1 (NOLOCK) ON ITE1.CODPROD = PRO1.CODPROD
             WHERE CAB1.TIPMOV = 'D'
             AND   MONTH(CAB1.DTFATUR)= DATEPART(MM,GETDATE())
             AND   YEAR (CAB1.DTFATUR)= DATEPART(YYYY,GETDATE())
             AND   ITE1.CODCFO IN (1202,1411,2202,2204,2411)
             AND   CAB1.STATUSNOTA = 'L'
             AND   PRO1.AD_MARCA=14)) AS REALIZADO,

             (SELECT DISTINCT 
              MET.PREVREC AS META_VN
              FROM SANKHYA.TGMMET MET
              LEFT JOIN SANKHYA.TGFPRO   PRO (NOLOCK) ON PRO.MARCA = MET.MARCA
              WHERE   MONTH(MET.DTREF)= DATEPART(MM,GETDATE())
              AND     YEAR (MET.DTREF)= DATEPART(YYYY,GETDATE())
              AND     PRO.AD_MARCA = 14) AS META
            )A

        </snk:query>

JavaScript snippet:

<div class="squareWhite" style="width:380px; font-size: 20px; height: 160px;">
                    APPLE<br></br>
                    META: <c:out value="${AP.rows[0].META}"/><br></br>
                    REALIZADO: <c:out value="${AP.rows[0].REALIZADO}" + "%"/><br></br>
                    ACUMULADO: <c:out value="${AP.rows[0].ACUMULADO}" /> //ESSE CAMPO DEVERIA RECEBER A PORCENTAGEM 
                </div>

Result:

REALIZADO   META        ACUMULADO
5641673,96  12500000,00 45,13339168

Accumulated should return as 45%.

Could you help me?

    
asked by anonymous 27.07.2018 / 22:12

1 answer

2

Rounding Forms

First you have to keep in mind that there are several possible ways of rounding up a number. I see that in your code, you use SQL, JavaScript and also Java since you're using JSTL, so I'll consider the forms of rounding existing in those three languages. Since your numbers are always positive, you'll probably want one of the following ways:

  • Up, toward + ∞: 2.1, 2.5, and 2.9 are rounded to 3. 3.5 is rounded to 4. In JavaScript and Java it is Math.floor(x) . In SQL it is FLOOR(x) .

  • Down, toward -∞: 2.1, 2.5, and 2.9 are rounded to 2. 3.5 is rounded to 3. In JavaScript and Java it is Math.ceil(x) . In SQL it is CEIL(x) .

  • For the nearest integer, with draws toward + ∞: 2.1 is rounded to 2. 2.5 and 2.9 are rounded to 3. 3.5 is rounded to 4. In JavaScript and Java is Math.round(x) . In SQL it is ROUND(x) .

There are other forms of rounding beyond this, especially as regards tie-breaking rules (towards + ∞, -∞, nearest pair, odd odd, or something else) and negative numbers (if rules are used equal or inverse to positive), but I find it unlikely that any of these other forms is the one you want.

Solution in SQL

The easiest way to resolve this is to round up the SQL itself:

ROUND((A.REALIZADO / A.META) * 100) AS ACUMULADO

Instead of ROUND , you can use CEIL or FLOOR if you prefer.

If you do not want to use the solution via SQL because you can not for some reason change the SQL, and you want a solution that is actually in JavaScript or JSP, then leave a comment.

    
28.07.2018 / 15:50