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
  • 25,191
  • 47
  • 61
  • 93
Marek123
  • 1,153
  • 6
  • 34
  • 71

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
  • 16,896
  • 10
  • 34
  • 63
0
str_replace(",",'',$number);

use this

Govind
  • 23
  • 5