Vivid Kothari
Vivid Kothari

Reputation: 13

Space is not coming when formatting string in php

When I am using printf, I am not being able to give space.

This is my code:

$number1=12.30;
$number2=1;
$f_number1 = sprintf("%9.2f",$number1);
$f_number2 = sprintf("%9.2f",$number2);
echo $f_number1;
echo $f_number2;

I am getting simple this:

12.30
1.00

And I want this instead:

    12.30
     1.00

Notice that there are spaces before the numbers.

Now when I do this:

printf("%'x9.2f", $number1);

then I am getting x instead of space. Like this:

xxxx12.30

My question is how to get space instead of x?

Thank you in advance, Vivid

Upvotes: 0

Views: 159

Answers (2)

Amir
Amir

Reputation: 4111

use html pre tag. Preformatted text.

<pre>
<?php
$number1=12.30;
$number2=1;
$f_number1 = sprintf("%9.2f\n",$number1);
$f_number2 = sprintf("%9.2f",$number2);
echo $f_number1;
echo $f_number2;
?>
</pre>

Upvotes: 0

Baba
Baba

Reputation: 95103

There is nothing wrong with your code .. you are just displaying it in html instead of text format

header("Content-type: text/plain");
printf("%9.2f\n", 12.30);
printf("%9.2f", 1.00);

Online Demo

For HTML then use &nbsp; with str_pad instead

Upvotes: 1

Related Questions