All Questions

Tagged with
50 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
2votes
1answer
2kviews

Laravel Eloquent float value stored in database returned additional numbers

This is my migrations $table->float('odd', 10, 0)->nullable(); I save odd value in db as 4.45 but it returned as 4.45000000000000017763568394002504646778106689453125 I look this post Laravel ...
user avatar
2votes
1answer
2kviews

Encoding and decoding floats in json with PHP without losing precision

I want to decode a json string to PHP object and then the object back again to json string without losing precision for floats numbers in json. If you run the sample below the output would be: JSON: ...
user avatar
2votes
1answer
1kviews

Reliably detect integer overflow/underflow

I'm working on code that has to do the following with the result of a calculation: If the result exceeds the limit that can be represented in PHP's integer type then throw an exception. If the ...
user avatar
  • 30.1k
1vote
0answers
125views

PHP unpack float return unexpected answer

I have some kind of binary data. I try to get the Float from that data. We already have the Java version for that program. So we already know what result should we get. When we try with the following ...
user avatar
1vote
1answer
142views

Reliable Margin of Error for Float -> String -> Float Conversion?

I have a float value that I need to store as a string in PHP and then compare later after casting back into a float. Due to the conversion I know that relying on equality would be a mistake, as there'...
user avatar
  • 2,877
1vote
1answer
1kviews

php round number - When echo it displays 2 decimals, if print to CSV it displays .0999999

Using http://www.maatwebsite.nl/laravel-excel/ to export to CSV and Excel. I have a $rows array wich contains all rows with it's columns. Some columns have a formatted number using number_format(...
user avatar
  • 183
1vote
0answers
51views

How to set float column value with php activerecord?

I have a table column price FLOAT(10, 2) NOT NULL DEFAULT 0.00 And Model: class Mytable extends \ActiveRecord\Model{ static $table_name = 'my_table'; } In php i use $price = 23.54; $obj = ...
user avatar
  • 429
1vote
2answers
537views

Inserting float in DynamoDB with PHP

I'm in the process of a shift from MongoDB to DynamoDB and one of the challenges I still haven't found a solution to, is inserting floats. I'm trying to do something like this: list($usec, $sec) = ...
user avatar
0votes
0answers
40views

How to get consistent result while dealing with float in php

Here is the code: $nums = ['3','3','5.26','3','9.11','9.11','9.11','9.11','9.11','9.11','9.11','9.11','9.11','1.25','1.25','1.25']; $total = 0; $accuracy = 0.00001; foreach($nums as $num) { $total ...
user avatar
  • 1,099
0votes
0answers
76views

Twig fixed point arithmetic

I'm trying to do precise arithmetics calculations inside a Twig template, but I am struggling with floating point's precision error and I can't find a way to solve this. Is there a way to solve this ...
user avatar
0votes
0answers
28views

Which way to compare two floats with n decimals is the most efficient in PHP?

It is known that comparing floats is a bit different story than comparing integers or strings, there are many posts explaining that, e.g.: How should I do floating point comparison? Can you safely ...
user avatar
  • 79
0votes
0answers
39views

Subtraction result is inaccurate when stored in a column with a datatype double in postgresql

I have this update query in laravel where I subracted a value to a total_qty column with a double datatype. Example: When 250 is subtracted by 15.1 the result should be 234.9 but in the total_qty with ...
user avatar
  • 1,198
0votes
0answers
48views

String to float conversion not working php

I've a string value of the type "x.yy" which I want to convert to double/float. I've tried floatval(), doubleval() and float and double type casting as well but it only gives me "X"...
user avatar
0votes
3answers
104views

Floating number upto two decimal PHP

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 ...
user avatar
0votes
0answers
58views

Why does 0.32 round up to 1 when multiplying

this is not about floating point math in general being broken... it IS NOT THE SAME ISSUE - this is likely an automatic type conversion - not float precision issue: So I'm trying to write an a small ...
user avatar
  • 751
0votes
0answers
150views

Why is the parameter type of the bcadd method in PHP a string?

When I look at the description of floating point numbers in the php documentation. I get a warning like this: I clicked on the link about arbitrary precision math functions because I need higher ...
user avatar
0votes
0answers
31views

Find FLOATS between FLOATS query with MySQLi PHP?

I'm working with latitude and longitude to get points that are closer to others points. I need to find the latitudes that may exist between 2 numbers in my DataBase, unfortunately I can't find -as an ...
user avatar
  • 507
0votes
0answers
76views

php arithmetic on monetary values

I have a form which has following inputs: <input type="text" name="amount" id="amount" maxlength="10" onblur="applyInterest();" /> <input style="width:60px" type="text" name="interest" id=...
user avatar
0votes
1answer
39views

Need to cast string value representing a 0.XXXX floats to actual number value in PHP

So I am trying to cast a string value of ie: '0.0000143' to actual FLOAT or DECIMAL number value (so it must not be a string after conversion), but the actual number of 0.0000143 as it needs to be ...
user avatar
0votes
0answers
94views

Sub-pixel positioning with php

I'm writing a graphic app in PHP. I'm using the GD function imagecopy() to combine two images, one smaller than the other. I want to position the smaller image on top of the bigger image in a float ...
user avatar
  • 11
0votes
1answer
57views

Does the value of floatval('a.bc') exactly equals to abc/100?

For example, I know 0.1 is not accurate due to floating point rounding error, however, I have another question: does $num=floatval('0.1'); exactly equals to $num=1/10 ? Do 2 $num above rounds to ...
user avatar
  • 3,918
0votes
2answers
406views

How to find out the size of a float in PHP?

I'm using PHP 7.2.5(installed using the latest version of XAMPP) on my machine that runs on Windows 10 Home Single Language 64-bit operating system. As per my knowledge, in PHP, the sizes of an ...
user avatar
0votes
0answers
347views

Using json_encode ( JSON_NUMERIC_CHECK ) with long floats in PHP

I'm encoding a JSON file with the following function: json_encode( $config, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK ); The issue is (or I'm pretty ...
user avatar
0votes
0answers
504views

Extra precision for floatval in php

I am running a piece of code to convert a string to a float on localhost ie, wamp server $row['lat'] = floatval($row['lat']); // Output : 15.2904195 <-- preferred format The same code when run ...
user avatar
0votes
0answers
329views

bcadd function not adding correctly

I have a problem in my php code where it didn't sum two floating point numbers. For this below code snippet it shows the output of "0.00108225". print_r(bcadd(0.00108225, 0.00008892,8)); It shows ...
user avatar
0votes
1answer
728views

PHP: format floats with given precision in json_encode() function

There was my question (initially not so accurate formulated): I need to use PHP floats in JSON string. Code: $obj['val'] = '6.40'; json_encode($obj); is converted to: {"val": "6.40"} It's OK - I ...
user avatar
  • 1,043
0votes
0answers
58views

php acting funny with large numbers

During one of my college competition the problem stated to take input from a file as numbers and then simply add half of it to itself. The numbers were 18 digit long integers. Most of my php output ...
user avatar
0votes
1answer
53views

PHP Find the first scientificly significant 'non zero' digits of an floating number

In this question, I am using the word significant in a scientific context I want to retrieve the first n digits from the left of a floating number. The function should take in: the $number which is ...
user avatar
0votes
2answers
41views

Converting a string to float

I am struggling with an issue where I have a string value '62.5816993713379' which when trying to convert to a float value gives me a different result. Please consider the code below: <?php $str ...
user avatar
  • 1,979
0votes
1answer
60views

Float number and zero comparison

I have a simple PHP task, to sum all number's digits. $number = 345; $digit = $number; $sum = 0; while ($number > 0) { $digit = $number % 10; $sum += $digit; $number /= 10; } This ...
user avatar
  • 3,621
0votes
1answer
55views

How to check a number or decimal or string would translate to an integer without round up

How to check a number or decimal or string would translate to an integer without round up? e.g: $value = 15.00000 // true $value= 15.00001 // false $value = '15.00000' // true $value= '15.00001' // ...
user avatar
  • 3,905
0votes
2answers
96views

How can I get a output with real number of digits in floating point portion from bcdiv function?

The problem is I don't know about the number of digits in floating point part in the output.. I can't assign 4 for example.. there might be 100 digits in floating point for instance n. How can I solve ...
user avatar
0votes
1answer
249views

Encode string to json number in PHP with JSON_NUMERIC_CHECK without losing presicion

If you run this code on PHP 5.3.10 in a 32 bit system (Windows7): <?php $object = new stdClass; $object->flaotAsString = "8226347662.8374"; $json = json_encode($object, JSON_NUMERIC_CHECK); ...
user avatar
0votes
0answers
373views

Using the bcmath functions correctly

I've never used the bcmath figures before and having been doing a bit of reading about comparing and manipulating floating point numbers and this has led me to the bcmath functions. I understand you ...
user avatar
  • 17.9k
0votes
0answers
94views

PHP DateTime object's diff() method causing issues with float variables

PHP v 5.3.5 I am having an issue where DateTime->diff seems to be mucking around with variables (which are type float) that it has no business touching. I'm obviously not understanding some way the ...
user avatar
  • 46
0votes
0answers
574views

how to store float numbers in mongodb php

I have a well-known issue about floats in php, but still can not get good resolution for it. I'd like to work with user's balance, that can be very little, actually. For example. Balance is 0.00015 ...
user avatar
0votes
1answer
158views

How to get readable value from columns with data type real in mssql

I have some Spec data(LSL/USL) something like this, with data type real, if I query with MSSQL Server Management Studio(MSSMS for short), it's displayed as 0.45, but if I query with php via sqlsrv ...
user avatar
  • 51
0votes
0answers
30views

Different floating point delimeter on servers

I have a windows pc with xampp installation where i develop my application. Floats will be displayed like 1.234. Iam from germany and i think it would be better to display them like 1,234. When i ...
user avatar
0votes
0answers
133views

fixed point datatype with PHP 5

Am I wrong (I hope), but I don't find on PHP a fixed-point datatype !? Datatype like DECIMAL/NUMERIC from MySQL will be nice for doing operations on money without precision loss. Any ideas ?
user avatar
  • 3,526
0votes
4answers
6kviews

Sum of floating point values in array in php

$gtz="select * from add_to_cart where user_id='$userid'"; $gz1=mysql_query($gtz); while($gz2=mysql_fetch_array($gz1)) { $tprcz[]=$gz2['total_price']; } $fullquantityz=array_sum($tqtyz); ...
user avatar
  • 117
0votes
1answer
3kviews

PHP floating point comparison with greater than or less than operators

I have read that you shouldn't compare PHP floating point numbers directly due to the possibility of floating point precision errors. I have a floating point number and I need to set a variable ...
user avatar
  • 11
0votes
2answers
813views

PHP: string with decimals strips decimals when converting to float

With the function floatval() I try to convert a string with decimals to a float, but it doesn´t give me the desired result. Here´s the code: $price_calc = str_replace(',','.',$price); $...
user avatar
0votes
2answers
1kviews

php compare float numbers with input field value

I have a form in which users can enter floating point values. I post these values to a php script and i compare if the numbers the users entered are between some values. If i post an integer the ...
user avatar
  • 2,543
0votes
1answer
1kviews

Selecting float number from FLOAT() column with PHP and mysqli

Good day. I have a problem with selecting float number from MySQL database using bind variables of mysqli class. The type of price column is FLOAT(9,2) and it's value 1.01 Simplified piece of code: ...
user avatar
  • 629
0votes
1answer
94views

What constant to select to compare result of fmod function

I need to check if one float/double is multiple of another float/double. In integer it is easy $isMultiple = $x % $y == 0; but in floats/doubles it does not. The first problem is that floats has no ...
user avatar
  • 3,200
0votes
2answers
3kviews

Convert String to Float up to 2 Decimal places

I have a php variable which contains values like '1.15' in string datatype, Basically i want to convert the string value to a Float value. When the variable contains value like 1.15 it gets converted ...
user avatar
  • 471
-1votes
0answers
28views

Laravel decimals; 55.0 Inserting into MySQL as 54 every time

I have two variables in PHP $current_price = 110.0; $coupon_discount = 55.0; I create a new variable $discounted_price $discounted_price = $current_price - $coupon_discount // 55.0 Dumping out the ...
user avatar
-2votes
2answers
155views

PHP Optimization: Integer instead of float

I have a PHP script that have a lot of calculations, loops e.t.c. So I have to optimize the code, because one AJAX request takes a lot of time (for example 20sec). When I look at the "echo" text (...
user avatar
-2votes
2answers
432views

PHP float number with decimal point rounding

I am calculating few numbers (sales_total, service_charge) to get 7% GST. What I get is number with decimal point. Example 1: $sales_total = 207.50; $service_charge = 20.75; $gst ...
user avatar
  • 3,470
-2votes
1answer
32views

Can an integer divided by another integer really become a float? Or is this a bug?

I had this: $test = (1590962400 / 1000); var_dump($test); It outputs: float(1590962.4) This really puzzled me, because my math knowledge seemed to tell me that two "solid" numbers like ...
user avatar
  • 1