A very small exponential number in php - php

I have a very small exponential number and it display like:
5.2255534523412e-7
Can I display it become:
5.22e-7

Use sprintf() to format the output
http://php.net/sprintf
$a = 5.2255534523412e-7;
// change 2 to be how many decimal places you want. The e treats it as scientific notation.
echo sprintf('%.2e', $a);

Related

Convert a string containing a number in scientific notation to a normal string in PHP

I need help converting a string that contains a number in scientific notation to a normal string.
Example strings: "9.1892E+11" to "918919850755"
PHP should understand the scientific notation.
By substracting or adding 0 or something, it will change to a normal notation.
By doing this you might lose precision.
You can cast the string to a float to prevent this.
$number = (float)$number;

format a floating number using PHP

I want to format a floating number, like this :
Input : 1.7
output : 01.70
I have already tried below function.
sprintf("%02.02f", 1.7);
Please help.
Try:
sprintf('%05.2f', 1.7);
Explanation
This forum post pointed me in the right direction: The first number does neither denote the number of leading zeros nor the number of total charaters to the left of the decimal seperator but the total number of characters in the resulting string!
Example
sprintf('%02.2f', 1.7); yields at least the decimal seperator "." plus at least 2 characters for the precision. Since that is already 3 characters in total, the %02 in the beginning has no effect. To get the desired "2 leading zeros" one needs to add the 3 characters for precision and decimal seperator, making it sprintf('%05.2f', 1.7);
Try this
sprintf('%05.2f', 1.7);
Are you tried with str_pad()? It's for strings, and that's what you need, because $var = 001 is an octal and $var = "001" is a string.
$input = 1.7;
$output = str_pad($input, "0", 2, STR_PAD_BOTH)

PHP formatting of positive and negative floating point numbers with leading zeros

Scenario:
To trim leading zeros from positive and negative floating point numbers
Input:
000.123
00.123
01.123
-001.123
-00.123
-040.123
Desired output:
0.123
0.123
1.123
-1.123
-0.123
-40.123
Question:
Is there an inbuilt function which will make this specific formatting easier and more efficient than running each number through combinations of substr(), strpos(), explode() and if statements?
I guess your numbers are saved as a string, so in order to get your output you just simple cast them to a float or double like this:
echo (float) $number;
For more information about casting see the manual: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
Just cast it as float
Like this example:
<?php
$number = '-000.220';
echo (float)$number;
This way you remove all leading zeros, either being a positive or negative number

php scientific notation significant digits

In the PHP manual for function sprintf I find:
e - the argument is treated as scientific notation (e.g. 1.2e+2). The
precision specifier stands for the number of digits after the decimal
point since PHP 5.2.1. In earlier versions, it was taken as number of
significant digits (one less).
Now I'm on PHP 5.4, but would like to format a number with scientific notation and indicate the number of significant digits instead of decimal places. Is there a function for this, or should I write it myself?
I looked over the (one less) part, that's all I needed.
thanks for the help!
This means that I just had to decrease the value of the precision by one to get my function to behave the way I wanted.
See here for the function I came up with tot support scientific notation using sprintf
function sn($number,$precision = 1,$case = 'e'){ //scientific notation
$precision--;
$string = str_replace('+','',sprintf('%.'.$precision.$case, $number))." ";
return $string;
}

number with no quotation in php

the php code is pretty simple :
<?php
$my_bn_num=number_bangla(0123);
function number_bangla($num1){
echo ("<br>num =".$num1);
}
?>
And the ouput is: num=83
But if I call the function with a singly quoted string like this:
$my_bn_num=number_bangla('0123');
the output is: num=0123
What is the detailed difference between 0123 and '0123' here ?
0123 is an octal integer because it starts with 0.
Integers will be printed by echo/print as decimal numbers.
'0123' is a string, so nothing will be converted when it is printed.
0123 is a numeric value whereas '0123' is a string value. Internally PHP stores those types differently.
In order to cast string to integer you can do following:
$num = (int) $num;
In this particular example printing number 0123 results to 83 because 0123 is an octal value. For casting octal strings to octal numbers PHP offers octdec() function:
$num = octdec($num);
0123 is an octal number. In most scripting languages any number represented by a 0 in front of it is treated as if it were an octal number (since you normally don't put 0's in front of numbers.
When you specify it in quotes its treated as a string.
When you are handling integers and want them in a particular format, what you should do is use the intval function.
intval($num,10) would translate the variable $num to a base-10 integer, instead of relying on the PHP casting code to work its black magic.

Categories