Convert string into float PHP - php

I have a column in a MySQL table that is a varchar. However the data in the column is representative of a float value. I want to know if there is an easy way to convert the string into a float without loosing the data. The problem is that there are leading zeros's that are complicating things for me.
Sample data:
"100"
"002"
"075"
"0300"
"0135"
Need to convert to:
1.00
0.02
0.75
0.300
0.135
When I try to convert by multiplying by a decimal the leading zeros are stripped off and 002 become 2.0 instead of 0.02. Complicating things more some of the string values are 3 characters and some are 4 characters.
I am using PHP 5.4.
Thanks for any assistance.

select cast(insert(your_column, 2, 0, '.') as decimal(10, 3))
from your_table

Try this code, it simply do casting. hope it helps.
$Myfloat = (float) $String;

Related

Laravel Schema Builder not setting the right digits amount on Double datatype

So, I need to save amounts that go up to 999,999,999,999.99, and in the documentation of the Schema Builder of Laravel says that I can set up up to 15 digits and 8 decimals, but this is not working (https://laravel.com/docs/5.2/migrations#writing-migrations)
In Column Types says:
$table->double('column', 15, 8);
DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.
The line of code in my migration is the following:
$table->double('m1',12,2)->default(0)->nullable();
Any ideas? Thank you.
try use:
$table->decimal('m1',12,2)->default(0)->nullable();
if your values only positive... then use:
$table->decimal('m1',12,2)->unsigned()->default(0)->nullable();
works to me!

Mysql Changing My Float Values

I have a database that stores lat and long coords, along with other fields.
The lat and long are set to float 10,8.
When running the following command ...
INSERT IGNORE INTO records(unique_id, city, st,zip,lat,lon) VALUES
( '80936EN476', 'West Jordan', 'UT', '84088-5205', '40.59660', '-111.963' )
The insert is completed with no messages, but when the record is retrieved, the "lon" field has been set to -100.000000.
When I try to edit the value in phpMyAdmin, I get the following error.
Warning: #1264 Out of range value for column 'lon' at row 1
You need to put more digits in front.
10,8 means 2 digits before decimal and 8 decimal digits.
try putting something like 12,8
Do not use (m,n) on FLOAT or DOUBLE. It leads to an extra rounding. And, in your case, it leads to truncation in the upper digits.
Ok, the issue turned out to be the float values.
I changed it to 10,6 and that resolved the issue!

numbers issue after export mysql data to CSV

when i get data from database and export it as a csv file i have following issues :
the leading zero of the numbers dosnt show
big numbers shows like this -> 1E+12
how to force MS Excel to show all of my data as string by PHP
You can use double quotes contain the numbers, like this:
if(is_numeric($column)) $column = '"'.$number.'"';
This isn't a MySQL issue, it's an Excel thing.
This... might help: http://answers.microsoft.com/en-us/office/forum/office_2007-excel/disabling-scientific-notation/943b8103-8c50-451d-8037-c697422e2307
But this question is more MS Office related than programming.
(or as comments suggested, use a file format that carries display formatting info, such as .xls)
I format it as a string by concatenate it with spaces at its start and end.
CONCAT(" ", database_number, " ") AS "Number For Excel".
Kai's answer didn't quite work for me. I ended up CONCATing an equals sign (still with the double quotes on either end) to the front and got the desired result. Our users wanted to be able to copy and paste the number straight out of Excel into our UI.
CONCAT('="',too_long_number_field,'"')
You can use number_formate() function to convert ...
View link
Function : number_format()
Syntax : number_format ( float $number , int $decimals = 0 , string $dec_point = ‘.’ , string $thousands_sep = ‘,’ )
Example to convert 6.90743E+11 to number use below code
number_format(6.90743E+11,0,'','') // outputs 690743000000

mysql,How to save rounded numbers into longtext field?

I have done this calculation:
$number = round(count($exc)/ count($grades),2,PHP_ROUND_HALF_UP);
When save,I have to save it into a field of an existing table which is a longtext type. The numbers got saved but become very long, something like 0.200000000000000011102230246251565404236316680908203125
I think this might cause extra db storage. When I query the numbers, they are still rounded as 2 digital. So, that's an extra converting job. I would like to learn how to save those numbers as rounded number?
You can use PHP sprintf function coupled with format specifier like so:
$number = sprintf("%.3f", round(count($exc)/ count($grades),2,PHP_ROUND_HALF_UP));
If you need more info about that take a look at sprintf PHP documentation here http://it2.php.net/manual/en/function.sprintf.php .

BIGINT in MySQL Changes a my number? Why does it do this?

I'll try and keep this simple. I'm using a BIGINT data type on a MySQL database table. I have a function generating a unique random number that may span anywhere from 12 digits to 13 digest long.
When I insert into the database with a digit that is 12 digits in length, it enters it just fine,
but when I use a value that is 13 digits or longer, it seems like it rounds up or something. here is the
php
$defaultText = 'some string'; //just some string
$web_id = 12;
$element_id = 23112182735363; //case 1 (doesn't work)
//$element_id = 2311218333205; //case 2, does work ()
mysql_query("INSERT INTO tableName (web_id, element_id, content)
VALUES ($web_id, $element_id, '".mysql_real_escape_string($defaultText)."')");
results:
in case one, it inserts a slightly different number, usually rounds up for some reason.
in case two, it inserts the number just fine! Maybe someone can help shed some light on this mystery! Thanks again!
the big int datatype:
bigint(200)
Numbers lose their precision from PHP_INT_MAX onwards. See also: http://www.php.net/manual/en/reserved.constants.php#constant.php-int-max
After that they are turned into floats which have limited precision and causes weird rounding issues. The only way to support BIGINT in PHP is by using strings.
I assumed you were talking about a 32-bit server.
But in my server, PHP seemed not lose the precision.
echo(PHP_INT_MAX . "<br/>");
$big_int = -6174803190685607000;
echo($big_int . '<br/>');
output
9223372036854775807<br/>-6174803190685607000<br/>
Sadly I still got the precision losing. I guessed it might because i used 'i' in prepare statement of mysqli, but I could not prove it.

Categories