3

I have the below problem, how to make it appear 0.00004 as string after multiplication?

$v = -0.00004;
$v = $v * -1;
echo $v; // 4.0E-5

Long story

As a result I need to save into MySQL database but no problem with that because I use below query:

$sql_insert = $conn->prepare("insert into tbl_transaction (amount) values ($v)");

However when I output to my HTML it showing 4.0E-5. I tried to use number_format but in some cases the decimals will go far more than this so it is not a choice.

Isaac
  • 99
  • 11
  • 3
    Have a look at the number_format() function – halojoy Dec 21 '17 at 18:35
  • MySQL understands exponential format, what's wrong with `4.0e-5`? – Barmar Dec 21 '17 at 18:49
  • @Barmar Problem is I need to display `0.00004` correctly in HTML – Isaac Dec 21 '17 at 18:50
  • Then why does the question say the problem is with saving it into the database? – Barmar Dec 21 '17 at 18:51
  • Use `number_format` with a large number of digits, e.g. `number_format($v, 20)` – Barmar Dec 21 '17 at 18:52
  • Are you kidding? I said no problem on saving that. It's problem when I output to HTML.. – Isaac Dec 21 '17 at 18:52
  • I see, I misread. – Barmar Dec 21 '17 at 18:52
  • number_format($v,20) return 0.00004000000000000000; Not ideal man :( – Isaac Dec 21 '17 at 18:53
  • The database is irrelevant to this problem, take it out of the question to avoid confusion. – Barmar Dec 21 '17 at 18:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161760/discussion-between-isaac-and-barmar). – Isaac Dec 21 '17 at 18:53
  • This is not a database problem, this is a displaying things *from* the database problem. – tadman Dec 21 '17 at 19:27
  • **WARNING**: Whenever possible use **prepared statements** to avoid injecting arbitrary data in your queries and creating [SQL injection bugs](http://bobby-tables.com/). These are quite straightforward to do in [`mysqli`](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [PDO](http://php.net/manual/en/pdo.prepared-statements.php) where any user-supplied data is specified with a `?` or `:name` indicator that’s later populated using `bind_param` or `execute` depending on which one you’re using. – tadman Dec 21 '17 at 19:27

4 Answers4

0

Try this.

echo sprintf('%f', $v);

Or:

$b = sprintf('%f', $v);
echo $b;
Dmytro Huz
  • 1,514
  • 2
  • 14
  • 22
  • Cant, it is returning `0.000040`. – Isaac Dec 21 '17 at 18:40
  • `echo sprintf()` is an "antipattern". There is absolutely no reason that anyone should ever write `echo sprintf()` -- it should be `printf()` without `echo` every time. – mickmackusa Apr 16 '22 at 03:16
0

You can try as below.

$v = -0.00004;
$v = $v * -1;
echo number_format($v, 5);

Hope this will help.

Aftab H.
  • 1,517
  • 4
  • 13
  • 25
0

After formatting the number with the desired precision you need to strip the trailing zeros.

This should do the trick:

$v = -0.00004;
$v = number_format($v * -1, 20);
print rtrim($v, 0);
marcell
  • 1,498
  • 1
  • 10
  • 22
0

If you var_dump($v) after the first assignment, you'll see that the "problem" is NOT the calculation.

$v = -0.00004;
var_dump($v);  //float(-4.0E-5)

number_format() can be used to return a formatted version of your value. You can get the length of the string value, and pass that to number_format.

var_dump(number_format($v,strlen(strval($v)))); // string(10) "-0.0000400"

So, your code would be:

$v = -0.00004;
$v = $v * -1;
$v = number_format($v,strlen(strval($v)));
echo $v  //0.000040

You don't need to use number_format until you're ready to display the value because the exponential number is the same value as the formatted / decimal number.

devlin carnate
  • 8,309
  • 7
  • 48
  • 82