Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Have you tryed the php number_format? Example from manual:

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
//  will give  1234.57
0

My problem is that eloquent give me the rounded value and not the original value. I want the original value.

0

Is 2.95 or 3 being stored in your database, check this. If 3 is being stored something is wrong. But if 2.95 is being stored, the above will display it correctly for you. Have you tryed decimal type or do you require a float. Usually with a float you store a number like 143.98234, but at display time you may only want to show 143.98.
Also see https://github.com/laravel/framework/issues/7075 I don't know if this applies or not.

Last updated 9 years ago.
0

I don't want to store data, I want to read data. The value in the database is 2.95 and eloquent gives me 3.

0

I had the same issue; it started when I updated from PHP 5.4 to 5.6. Changing the column type to decimal or double solves it.

0

In view format it, and it will display correctly.

0

ricardofls: Can't edit the production database.

jimgwhit: So please tell me how you format a rounded value to its unrounded original ...

0

All I can say it's most likely a Laravel bug. It's not just simple rounding. I was getting 18000 instead of 17765.84 and 19000 instead of 18501, for example.

Laravel worked well on the test server which runs PHP 5.4, however, it "rounds" floats on my dev environment which runs PHP 5.6.

In my struggle I tested PDO and Doctrine DBAL; they both work well.

I'm sorry to say that your options are limited.

Last updated 9 years ago.
0

Skip using cast, retrieve data in a query, and just format it as wanted in your view. http://stackoverflow.com/questions/27651808/using-number-format-method-in-laravel. Remember blade is different in L5 than L4.

Last updated 9 years ago.
0

jimgwhit: Retrieving all data in a custom query is stupid because all eloquent magic is gone. Retrieving only the float values with a custom query is also stupid (5 or more queries for 1 database entry).

ricardofls: Thats not good. Can't display rounded prices in the frontend.

0

I didn't say a custom query, you don't know how to retrieve data with eloquent, then display that data in a view?

Last updated 9 years ago.
0

jimgwhit: Every eloquent function gives me a rounded float value. Model::find(1), Model::first, Model::latest()->get(). So how would you query the row with eloquent, get a Model instance and get the unrounded value?!!

0

Try ask Larave to return it as a string. IMHO it's string when comes from the database driver originally. Then convert the way you need.

Last updated 9 years ago.
0

neomerx: Tried it already, got "3".

0

I would check your database settings and try to get it by other tools. It's likely you really got 3 from your db.

0

Normal sql query gives me 2.95.

0

Stop using cast!

$mydata = Model::find(1),//or your actual way you are retriving the data (no cast)

Now display the data in the view, using the stackoverflow format number answer from above.
This should work.

Just do model similar to:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Powner extends Model implements AuthenticatableContract, CanResetPasswordContract {

	use Authenticatable, CanResetPassword;
    protected $table = 'powners';
    protected $primaryKey = 'ownerid';
    protected $fillable = [
        'ownerid',
        'oname',
        'ostreet',
        'odate',
        'ocheck'
   ];
        public $timestamps = [];
}   

This is where the problem is coming from:

protected $casts = [
        'id' => 'integer',
        'shipping_free_over' => 'float',
        'shipping_fee_value' => 'float(10,2)',
        'shipping_min_order' => 'float',
        'view' => 'integer',
        'deducted_prescription' => 'float',
        'dif' => 'float',
        'contract' => 'integer',
        'reseller' => 'integer',
    ];
Last updated 9 years ago.
0

No that don't work! That is what I do the whole time!

return Pharmacy::first(); //or find(1) ...

Model:

class Pharmacy extends Model{

    protected $table = 'pharmacy';
    public $timestamps = false;

    //protected $casts = [
    //    'id' => 'integer',
    //    'shipping_free_over' => 'float',
    //    'shipping_fee_value' => 'float(10,2)',
    //    'shipping_min_order' => 'float',
    //    'view' => 'integer',
    //    'deducted_prescription' => 'float',
    //    'dif' => 'float',
    //    'contract' => 'integer',
    //    'reseller' => 'integer',
    //];
}

Output:

{
...
shipping_fee_value: 3, //should be 2.95 as in the database
...
}
0

I just tested a float, I entered 3.14, and I am getting 3.14 back. If you want to see the model/view/controller let me know. Oh I didn't have to format the number, it showed correctly.

Last updated 9 years ago.
0

And I am not. It's a very big (and old) database. Main system written in Zend Beta on a php 4.4.9 server. Charset is utf8_general_ci (database and table).

I use the php development server (php 5.6.3) for local development.

Can't edit things on the database (main system would explode or so ...)

0

I did another test, This is just testing everything in controller.

public function numtest(){
       $mydata = \App\Powner::find(1);
        echo $mydata->ntest;
    }

ntest is a field I added as a float type.
I popped in a quick route for the test:

Route::get('ntest', array('uses' => 'PownersController@numtest'));

It's working on my machine. Recheck your model, mine is:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Powner extends Model implements AuthenticatableContract, CanResetPasswordContract {

	use Authenticatable, CanResetPassword;
    protected $table = 'powners';
    protected $primaryKey = 'ownerid';
    protected $fillable = [
        'ownerid',
        'oname',
        'ostreet',
        'odate',
        'ocheck',
        'ntest'
   ];
        public $timestamps = [];
}   

Do you have the protected $fillable part?

Last updated 9 years ago.
0

I do not need the fillable array because I don't want to store entries in that table.

0

Reply again if you get it working so we'll know. All I know is its working here without even having to format the number.

0

My advice would be:

  • run some raw SQL in Heidi or such like
  • stick a bunch of echos right in the laravel source code
  • work your way up from the DB drivers, to the DB class, to Eloquent

Find out exactly what's coming out of the database, and work out where it's being rounded.

Sounds like it might be a driver issue (not that I've read the whole thread, sorry if I missed stuff)

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.