PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to format my result for 2 decimals digits after comma

 August 11, 2022     decimal, format, sql     No comments   

Issue

I'm kind new to SQL world and just started doing queries.

In this case, I'm calculating how much % a number represents of that other number.

This is how I'm doing:

Percentage = ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2),

Even if I use ROUND(), the results still the same, they will give me 6 digits after comma:

100.020000
56.800000
-33.330000
100.000000
42.490000

Is there any way to format these numbers with just 2 digits after comma?

Thank you!


Solution

Assuming you use sql server, you can convert your result :

Percentage = Convert(numeric(10, 2), ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2))

Or Cast it :

Percentage = Cast(ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2) AS numeric(10, 2))


Answered By - EddiGordo
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing