I have written a CSV importer and some of the values are very small, for example, in one of the CSVs I have 0.000024 which is being formatted as "2.40E-05" and when PHP puts that into the database it stores it as 2.4, I found an article that said if you add 1 to it, it changes to a float which is what I did and I got 1.000024, but if I then subtract 1, it goes back to being 2.4E-05, what is the best way in PHP to convert a scientific notation string to an actual float value? I would like to be able to store the value in my database as 0.000024.
You should convert to a string otherwise if you keep the float it will keep displaying with scientific notation
$scientific = "1.828331E-9" ;
$num=explode('-', $scientific);
$precision=$num[1]+ strlen(filter_var($num[0], FILTER_SANITIZE_NUMBER_INT))-1;
$float = number_format($scientific, $precision);
echo "converted $float and back to original " . (float)$float;
I just used number_format($revenue, 6) and it stored as 0.000024.
Related
I know the question is very basic but it seems nothing working for me.
I have a number (either or float or integer) which I want to be formatted upto two decimal point. For this purpose I'm using PHP function number_format but it converts my number to string.
To convert it back to float I am using (float) or floatval(). But these functions just truncates the number after converting it to float from string.
Here is my code
$revenue_sum = array_sum(array_column($val2, 'weighted_revenue')); //23722
$test = number_format($revenue_sum, 2); //"23,722.00"
$test = (float)number_format($revenue_sum, 2); //23.0
$test = floatval(number_format($revenue_sum, 2)); //23.0
I want the $test to be 23722.00 for the $revenue_sum = 23722
If $revenue_sum = 2372.2 the $test should be 2372.20
number_format() function can be used as follows:
echo number_format($revenue_sum, 2,'.',''); // will return 23722.00 for the $revenue_sum = 23722
You are trying to type cast with ',' value, it is truncating the string.
you can try this
<?php echo sprintf("%2.2f", 8900.258); ?>
which will output as
8900.26
If you assign a floating point value to a variable, then it is converted to an internal binary format (usually using IEEE 754). Not all possible values has an internal representation. So while scanning a text, the float is rounded to the nearest possible value. So for example 1.23 is rounded to 1.22999999999999998.
Because of the internal representation, there is no difference between 100 or 1e2 or 100.0 or 100.0000.
And when printing a floating point value without any formatting instruction, PHP guess a good format and rounding some digits. So 1.22999999999999 is displayed as 1.23(may varies on different systems).
In general: As long you are calculating, formatting doesn't matter. It is mostly the best, to ignore the decimal fragments on debugging. But when printing (=converting to text), use functions like format_number() or any of the printf() functions.
To be more pragmatic:
I'have a $value like that var_dump(bin2hex($value)) output = '015180'
I want the convert this value to decimal. So I know my $value is binary, I write those codes:
var_dump(bindec($value));// 0
var_dump(hexdec(bin2hex($value)));//86400
Result is 86400 but why don't the first one returns me 0. what is the different between two of them. Or Am I missing something ?
I'm only quoting one of comments in bin2hex documentation:
bin2hex function is for converting binary data into a hexadecimal string representation. This function is not for converting strings representing binary digits into hexadecimal.If you want that functionality, you can simply do this:
<?php
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;
?>
This would output "f9". Just remember that there is a very big difference between binary data and a string representation of binary.
Author: tehjosh
$td = 4.0;
echo $td;
The output is 4;
But I want real number (4.0) in double data type;
First, not to be nitpicky, but PHP doesn't have the type you want*. When you do $td = 4.0; you have created a float.
If you inspect it with var_dump($td);, you'll see: float 4. Since there isn't really a concept of significant figures here, the zero after the decimal is not relevant to the stored value.
Second, when you do echo $td;, PHP will output the string representation of float 4. Again, without somehow specifying that you want to display n decimal places, PHP will omit any trailing zeroes. For another example, if you did this
$td = 4.00010000;
echo $td;
You'd see
4.0001
This is why the other answers/comments are guiding you toward a formatting solution. Because what you're really needing to do is not to change the type of the variable, because it's already stored in an appropriate type. You just need to specify how it should be displayed when it's converted to a string. There are different ways to do that. If you use printf, you can specify a number of decimal places to display. Here's how you make it show one, for example:
printf('%.1f', $td);
The '%.1f' is a format string. The .1 part is what tells it to show one decimal place. But you aren't changing the type. It's just output formatting.
*Here's a list of PHP's native types. And I was sort of mistaken, it does indicate that float is aka double.
You can use printf
echo printf("%f\n", $td);
Check this out -> string number_format ( float $number [, int $decimals = 0 ] )
Doc: http://php.net/manual/en/function.number-format.php
$td = 4.0;
echo number_format($td,1);
this will spit out 4.0 the "1" is the number of decimals you want in the number
I have a string ($maxDeposit) which is a numeric monetary value. So, for example:
123.00
This string is being passed in to jQuery, it needs to be passed in as a numeric data type. I'm achieving this using the following:
$maxDeposit = floatval($maxDeposit);
This loses the last last decimal place however, so my number looks like:
123.0
I have this method of converting the number to two decimal places:
$maxDeposit = sprintf('%0.2f', round($maxDeposit, 2));
However this also converts the number back to a string. Is there a way I can convert the string to a float but keep the last decimal place? Thanks
No, float is a numeric value, and 123.00 is its representation with 2 decimal places. It is responsibility of view layer to format numbers. In your case it is jQuery, e.g. console.log(maxDeposit.toFixed(2)).
I think, You can use floatval/float and number_format.
$maxDeposit = number_format(floatval($maxDeposit), 2);
or
number_format((float)$maxDeposit, 2, '.', '');
http://php.net/manual/pt_BR/function.number-format.php
My $_POST value contains css friendly hex color value, for example: #ffffff. In order to make it friendly towards the ColorJizz library, I should have it in hexadecimal integer value. The code below doesn't work. If I replace the $color_hex variable with a hard coded value for example: 0xffffff, it works.
include('colorjizz.php');
$color_hex = '0x' . substr($_POST['color'], 1);
$color = new Hex($color_hex);
This is most likely a very noob level problem, but after hitting my head to the wall for a quite few hours, I'd be grateful for any advice. Thank you.
"Hexadecimal integer value" doesn't make much sense. Hex is a representation of a number (thus a string), while integer value speaks of the machine format of a number.
If you want the number, a quick Google search found hexdec
$color = hexdec(substr($_POST['color'], 1));
It appears to ignore leading "junk", so you could even use
$color = hexdec($_POST['color']);
If you have a string containing a hexadecimal representation of a number and you want to have it as a integer you have to convert from hex to dec. If I get your question right.
$dec = hexdec("ff0000");
http://php.net/hexdec
In PHP, the data type for a hexadecimal integer and a decimal integer is the same; being able to type 0xDEAD in your source code is just syntactic sugar. Thus, you can use intval() to convert the form input to a normal PHP integer from a string:
$color_hex = intval(substr($_POST['color'], 1), 16);
The final parameter, 16, specifies that the input string is in hexadecimal.