I have a simple question about variable type in php. I have two values in my array:
$row['DS'] // type :float (with one decimal like 12.2)
$row['TC'] // type :float (with one decimal like 24.2)
What I'm actually try to do in the make the calculation below:
$row['TC'] / $row['DS'] // $row['DS'] need to be as integer (without point,like 12)
and the result should be with two decimal like (2.32). I tried to do it in that way
$DSF = number_format($row['DS'],0);
$ConF = $row['TC'] / $DSF ;
echo number_format($conF,2);
but it returns the wrong result. for example :
$row['DS'] = 59,009.3 ---> after change the format is change to 59,009
$row['TC'] = 190.0
$ConF = 190.0 / 59,009
it should be 000.223 (something around this number ) and i expect to get 0 (after i change the format using number_format($conF,2) but instead of this the program return me the number 3.22
What am I doing wrong?
The function number_format() is used to format numbers to a comma style representation, not to actually round numbers to what you want.
The function you are looking for is round which returns a float to a specified number of decimal places.
For example:
$yourVar=round($row['TC']/$row['DS'],2);
This means that $yourVar will be the value of the division rounded to two decimal places.
You should use the number_format() function only to display human-friendly numbers at the end.
You can use type casting to convert $row['DS'] to integer right in your calculations, for e.g.:
$row['TC'] / (int)$row['DS']
or
$row['TC'] / intval($row['DS'])
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 number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.
I want to show a float number in php string.
something like:
My float number is: 0.00003485
But when I am using echo I see it like this:
My float number is: 3.485E-5
Also, I know I can use printf("%.10f",$float) code, but I need to use it in lots of places in my string, So I can't use printf.
What code should I use on a string, to show floats as they are? I don't want that shorted number (3.485E-5).
At some condition i am prefer declaration float number as string:
MyFloatNumber = "0.0000000000000000000000003485";
so,
echo MyFlatNumber;
//0.0000000000000000000000003485
I know I can use printf("%.10f",$float) code, but I need to use it in lots of places in my string, So I can't use printf.
I have to respectfully disagree with that:
printf('My float number is: %1$.8f (again: %1$.8f; once more: %1$.8f)', 0.00003485);
Or, if you need it in a string:
$foo = sprintf('My float number is: %1$.8f (again: %1$.8f; once more: %1$.8f)', 0.00003485);
Of course, if you mean it's against project style guidelines or company policy, that's another story.
What code should I use on a string, to show floats as they are?
Really? Floats in PHP are a 64-bit IEEE 754 double precision format bitmap. According to this online converter, number 0.00003485 would be:
0010010001010111110011100001110100101110111001001111
Of course, they aren't actual ones and zeroes but different voltage levels ;-)
You can write a function to handle this in any way you want. Why not?
function fval($f, $precision = 9) {
return number_format($f, $precision);
}
Then any place you need to format, call it out:
$f = 3.485E-5;
echo fval($f); // this will print: 0.000003485
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
I have a situation where all records in a CSV I'm parsing are currency, but the values are not separated by a decimal point. So for instance, value '1234' is actually '12.34', and '12345' is '123.45'.
I'm struggling to find a way to manually convert these values into decimals. I can't user number_format, because it will give me an output like so:
$original_num = 1234;
$formatted_num = number_format($original_num, '2', '.', '');
$formatted_num = 1234.00; //Output
The other issue is that sometimes I may have a value like '436257.5' after I combine two numbers, which is actually '436.2575' so I can't just manually push in a '.' two places from the end of the string. Should I consider formatting it differently while I'm parsing the file?
Assuming you're using integers to always represent decimals with 2 places of precision after the decimal point, you just divide with 100 to insert the dot in the right place.
What do you mean, "combine"? You mean multiply? You should renormalise after each multiplication, and never get into a situation where you're representing decimals of differing precisions (unless you keep track of the precision, which you can do but it's pain in the ass and normally unnecessary).
function multiply($a, $b) {
return round($a * $b / 100);
}
function format($a) {
return sprintf("%.2f", $a / 100);
}
Since number_format() function always adds 2 00 as decimal, you can divide the value by 100.
number_format($original_num/100,2);