Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

float driving me round in circles

Featured Replies

Hi Gang,

 

Probably really simple but here's what's driving me round in circles. What I want is to display a number to 2 decimal places with the option of floating one 0 if required. So here's what I got, none of which do exactly what I need.

// Number stored is 1.25
$number = 1.25;
echo (float)​$number; // makes 1.25 ideal!
echo number_format($number, 1, '.', ''); // makes 1.2 - not good.

// Number stored is 2.50
$number = 2.50;
echo (float)​$number; // makes 2.5 Ideal!
echo number_format($number, 1, '.', ''); // makes 2.5 ideal!

// Number stored is 1.00
$number = 1.00;
echo (float)​$number; // makes 1 - not good
echo number_format($number, 1, '.', ''); // makes 1.0 ideal!

What's glaringly obvious that I'm missing here?

 

Thanks

  • Author

 

Hi!

Have you tried:

 

number_format($number, 2, '.', '');

 

Yes but then it makes 1.0 become 1.00. To clarify I want the following to display;

 

1.00 display as 1.0

1.25 display as 1.25

2.50 display as 2.5

 

Thanks though :D

  • Author

Yeah I was looking into breaking the number apart and then fiddling with the decimal section however I really wondered if there was an out the box number format of some description already built into php. Thanks Nock, this is probably the route for now.

  • Author

Thanks Nock, made a little function for anyone else wanting to do this, aka the nockNumber ;)

function nockNumber($var) {
  $rumble = explode(".", $var);
  if(substr($rumble[1], -1)==0) {
    $var = $rumble[0] . "." . substr($rumble[1], 0, 1);
  }
  return $var;
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.