Show small numbers without exponential form in PHP and WordPress - php

Is it possible to show tiny numbers without exponential form?
<?php
$a=4;
$b=799999999999999;
$c=$a/$b;
echo $c;
?>
I use this code in a WordPress page (PHP enabled) and it outputs 5.0E-15 instead of 0,000000000000005
I use default Twenty Sixteen theme without custom functions.
How would you edit the PHP code above in order to show the correct number?

You can use number_format function for this.
<?php
$a=4;
$b=799999999999999;
$c=$a/$b;
$d = number_format($c, 15, ',', '');
echo $d;
?>
Outputs: 0,000000000000005
But as you stated, you need a more dynamic solution as the decimal place isn't fixed. So here's my proposed solution.
Long version:
<?php
$a=4;
$b=799999999999999;
$c=$a/$b;
$e = 0; //this is our output variable
if((strpos($c, 'E'))){ //does the result contain an exponent ?
$d = explode("-",$c); //blow up the string and find what the decimal place is too
$e = number_format($c, $d[1], ',', ''); //format with the decimal place
}else{
$e = $c; //Number didn't contain an exponent, return the number
}
echo $e;
?>
and here's the previous code shortened down a bit:
<?php
$a=4;
$b=799999999999999;
$c=$a/$b;
$d = (strpos($c,'E')) ? number_format($c,explode("-",$c)[1],',','') : $c;
echo $d;
?>
( I deleted my answer and reposted as I'm not sure if you got the alert that I amended my answer)

Related

PHP and dynamic number formatting

I have looked for a way to do this and have not found it.
I have values read from MySQL: 100.00, 85.50, 97.00, 71.33
I want them to display as: 100, 85.5, 97, 71.33
I see number_format() that specifies FIXED decimal places, but I need a sort of 'significant digits format'
use (float)$number;
$a = '100.00';
$b = 73.50;
$c = 71.33;
echo (float)$a; // 100
echo (float)$b; // 73.5
echo (float)$c; // 71.33
you need to use floatval function to get your required output. just check below code.
var_dump(floatval('100.00'));
var_dump(floatval('85.50'));
var_dump(floatval('71.33'));

trouble with calculating percentage using PHP

I wanted to calculate the percentage using PHP. I tried the code given below but its gives me the return value in float. i don't know much in PHP so please fix this code.
current OUTPUT
66.666666666667%
Expected OUTPUT
66.66%
<?php
$up=4;
$down:2;
echo (($ups/($ups+$downs))*100).'%';
?>
Use number_format() to specify your decimals and separator.
<?php
$up=4;
$down:2;
$num = (($ups/($ups+$downs))*100).'%';
$formatted_num = number_format($num, 2, '.', '');
echo $formatted_num;
?>
You can do like this :
echo round(66.666666666667, 2); >> 66.66

How can I check if number has one , or two decimals?

I would like to know how I can check if a number has one or two decimals, and if it only has one decimal , like 12,9 for example, then echo the number with an additional 0, so it looks like 12,90.
<?php
$number = '12,9';
if $number //has 2 decimals // {
echo $number; }
else {
echo $number.'0';
}
endif;
?>
I have no clue how to do that properly, any help would be really appreciated! Thanks
If your input is a . (dot) separated decimal, you can just use number_format():
number_format('12.9', 2);
Alternatively, you can use the NumberFormatter class if you need to support multiple locales or numbers with commas for decimal separators. Such as:
$formatter = new NumberFormatter('de_DE', NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 2);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);
echo $formatter->format($formatter->parse('12,9'));
Note: The use of NumberFormatter requires the intl extension. It can be added on debian based systems with a simple sudo apt-get install php5-intl.
If you're sure that the number is always formatted like you've posted, than you could do:
number_format(str_replace(',', '.', '12,9'), 2, ',', '.');
what you need is the number_format function
http://www.php.net/manual/en/function.number-format.php
Since PHP is not strictly typed, you could so something like this:
$parts = explode(",", $number);
$num_decimals = strlen($parts[1]);
if ($num_decimals == 2) //has 2 decimals // {
echo $number;
} else {
echo $number.'0';
}

php strip tags?

I was just curious about this to see how this might work.
$number = "3/4";
echo $number;
and I get 3/4 as a string
$number = 3/4;
echo $number;
and I get 0.75 because it's doing the math
I am curious to see if there is a way to strip the "/" from the first one and have it divide number before the "/" by the number after the "/" so that it would come out as 0.75 instead of 3/4.
The reason I am building a form for a person who wants them to input the number in decimal form. However if a person inputs 3/4 I am going to kick an error that asks them to input it in decimal form and give them the decimal number.
A simple function would look like something below, you can throw more error handling in there if you want also.
function makeDecimal($string)
{
$parts = explode('/',$string);
if(count($parts) != 2)
//throw error here
return intval($parts[0]) / intval($parts[1]);
}
You can explode() the expression using the "/" as the delimited then just do the math on the numbers.
<?php
$number = "3/4";
$num = explode("/", $number);
$a = $num[0];
$b = $num[1];
$result = ((float)$a) / ((float)$b);
echo $result;
?>
Hope this helps

PHP number format drops zero when rounding [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I'm using PHP's number_format to display floats to 2 decimal places.
When the number is something like 1.898 it gets rounded up to 1.9.
How do I get it to display that as 1.90?
Update:
I have a function that ends...
return number_formant($num, 2);
The php script that calls the function prints out the number to be used by Javascript. When I do a var_dump on the number, it prints correctly with two decimal places. Looks like it's Javascript that's loosing the zero.
Here's the JS code that was causing the issue...
function show_level(level) {
...
if (level > 9999)
level_label = (level / 1000).toPrecision(3) + 'k';
else if (level > 999)
level_label = (level / 1000).toPrecision(2) + 'k';
else
level_label = level;
I altered the last line to get it working how I wanted..
level_label = level.toFixed(2);
Maybe not the best solution but:
$n = 1.2345;
$n = number_format( round($n, 1), 2);
//echo 1.20
echo $n;
So,
you mean something like this (what you have):
<?php
echo number_format($x);
?>
and here is what you want:
<?php
echo number_format($x,2);
?>
you can use other lengths. Try it Out!
Hope could helpya
:)
echo sprintf('%01.2f', number_format(1.898,2))
You can do:
echo round(1.898,2);

Categories