1

i have problems adding a leading zero to a number_format() number:

$number = "6,0"; // coming as string from DB      
number_format((double)$number, 1, '', '')  

I need to get

  • 060 from 6,0 or
  • 150 from 15,0 or
  • 123 from 12,3 or
  • 012 from 1,2

Using

sprintf("%02d",$...); 

didn't help. Any other possibilities?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Marek123
  • 1,193
  • 7
  • 35
  • 75

2 Answers2

1

You'll need to get rid of the commas first:

$number = str_replace( ',', '', $number );

Then you can use str_pad as was suggested in this question, which Francesco Malatesta posted as a comment.

$number = str_pad( $number, 3, '0', STR_PAD_LEFT );

You can reduce it to a oneliner:

$number = str_pad( str_replace( ',', '', $number ), 3, '0', STR_PAD_LEFT );
Community
  • 1
  • 1
Schlaus
  • 18,144
  • 10
  • 36
  • 64
0
str_replace(",",'',$number);

use this

Govind
  • 21
  • 5