PHP: Round A Number Upto 2 Decimal Places And If The Number Is A Whole Number Then Add Trailing Zeros

Question


$number = 153.78999
$rounded_value = round($number,2);

$rounded_value will be 153.79

What i want is if

$number = 153

then

$rounded_value = 153.00

----------------------------------------------------------------------------------------------
Answers
----------------------------------------------------------------------------------------------
You can try like this:

sprintf("%0.2f",$number);


Check sprintf

or try like this:

$rounded_value = number_format($number,2);

Check number_format

0
0
181
Welcome to CodeXpress Forum Script - Download NL script Free


No comments. Be first to post a comment.

Reply to this article


Welcome, Guest: Join to reply login to reply!

♣ Post Statistic

1 guest(s) viewing this topic

Related Post

Php: Round A Number Upto 2 Decimal Places And If The Number Is A Whole Number Then Add Trailing Zeros

♣ CodeXpress Statistic

Welcome, Guest: Join CodeXpress Login

17 Members, 135 topics and 9 posts
Date: Friday 01 July 2022 at 02:43 AM

Top 5 Contributors

People who started the most discussions on codexpress.

  1. 17
  2. 92
  3. 3
  4. 11
  5. 1

Sections

CodeXpress / General:
Politics, Business, Investment, Career, Autos, Counties, Properties, Agriculture, Health, Travel, Family, Culture, Religion, Food, Pets, Romance, Diaspora, Africa, International Forum, World List Directory, How To,
Science/Technology:
Programing, Webmasters, Computers, Phones, Art, Graphics & Video, Technology Market, CSS, Innovation, PHP 7x, Vanilla JS, Software Updates,

♣ Users Online: (2)

0 Members and 2 Guests online in last 5 minutes!


Sections:

Food, Religion, Culture, Investment, Africa, Webmasters, Computers, Technology Market, Phones, Software Updates, TV/Movies, Fashion, Music/Radio, Sports, Poetry,

CodeXpress - Copyright © 2019 - 2022 Marshall Unduemi. All rights reserved. See How To Advertise.
Disclaimer: Every CodeXpress member is solely responsible for anything that he/she posts or uploads on CodeXpress.