How to format a number in percentage within Javascript?

Asked

Viewed 570 times

0

Gentlemen, I am developing a billing monitoring panel. I want to turn this number 56% directly into my Javascript code. Follow an excerpt of my code, it is directly fed by a query of my SQL.

This is the query that feeds my field into Javascript, the variable MT receives her.

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

Code snippet in Javascript:

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

Upshot:

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

The Accumulated should return as 45%.

Could you help me?

  • Complement: código no Javascript <div class="squareWhite" style="width:380px; font-size: 20px; height: 160px;"> MOTOROLA <br></br> META: <c:out value="${MT.Rows[0]. META}"/><br></br> REALIZED: <c:out value="${MT.Rows[0]. REALIZED}" /><br></br> ACCUMULATED: <c:out value="${MT.Rows[0]. ACCUMULATED}" /> </div>

  • 1

    It would be interesting if you took a look at tour to better understand how things work around here!

1 answer

2


Rounding forms

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

  • Upwards, towards + : 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 is FLOOR(x).

  • Down towards the - 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 is CEIL(x).

  • For the nearest integer, with ties towards + : 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 it is Math.round(x). In SQL is ROUND(x).

There are other forms of rounding in addition to these, especially with regard to tie-breaker rules (towards + , - nearest pair, nearest odd, or something else) and negative numbers (rules are used the same or inverse to positive ones)but I find it unlikely that any of these other ways are the one that you want.

Solution in SQL

The easiest way to solve this is to round up SQL itself:

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.