anon
anon

Reputation:

printing a float with fixed size of 0

Consider i have this number:

0.012
0.1
0.04
0.0011

i need them to have always 4 digits after .

Example:

0.0120
0.1000
0.0400
0.0011

Should i use printf?

%f1.4?

Upvotes: 0

Views: 299

Answers (1)

user142162
user142162

Reputation:

%.4f is what you're looking for.

The following will output the number to standard output or the web page:

printf("%.4f", $number);

Or if you want to dump it to a string:

$str = sprintf("%.4f", $number);

Upvotes: 3

Related Questions