1

I'm a bit puzzled by the situation I have now.

I've a simple SQL statement I execute from NodeJs on a SQLite database. The SQL statement returns values with a lot of decimals; although my data only contain two decimals.

When I run the exact same query in DB Browser for SQLite, I have a correct result.

My NodeJs code

app.get('/payerComparison/', (req, res) => {    
    // Returns labels and values within response
    var response = {};

    let db = new sqlite3.Database('./Spending.db', sqlite3.OPEN_READONLY, (err) => {
        if(err){console.log(err.message); return}       
    });

    response['labels'] = [];
    response['data'] = [];
    
    db.each("SELECT payer, sum(amount) AS sum FROM tickets GROUP BY payer", (err, row) => {
        if(err){console.log(err.message); return}

        response['labels'].push(row.payer);
        response['data'].push(row.sum);
    });

    db.close((err) => {
        if(err){console.log(err.message); return}
        // Send data
        console.log(response);
    
        res.send(JSON.stringify(response));
    });
})

What I have in the command line

{
  labels: [ 'Aurélien', 'Commun', 'GFIS', 'Pauline' ],
  data: [ 124128.26, 136426.43000000008, 5512.180000000001, 39666.93 ]
}

The result in DB Browser

Printscreen of query results in DB Browser

I hope you can help me clarify this mystery! Thank you

1 Answer 1

1

Round the values up to 2 decimals :).

SELECT payer, round(sum(amount),2) AS sum FROM tickets GROUP BY payer
Sign up to request clarification or add additional context in comments.

1 Comment

It does the job, thank you. Though I still doens't understand where does the difference come from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.