Laravel casting exponent to float not working


Laravel casting exponent to float not working



I have a very small number 0.00000064 that has the datatype of double(20,8).
I am seeing the number as 6.4E-7.


0.00000064


double(20,8)


6.4E-7



In my model I am trying to cast column as follows:


protected $casts = [
'total_crypto_balance' => 'float'
];



but I am still receiving that exponent number back.



to receive the number 0.00000064 in view and not the exponent 6.4E-7.


0.00000064


6.4E-7



What am I doing wrong here?





Possible duplicate of php- floating point number shown in exponential form
– Namoshek
Jul 1 at 7:26





@Namoshek - not really, nothing to do with Laravel casting. YET - if you can derive an answer from that into a viable Laravel eloquent solution, I will vote you as answer.
– erezt
Jul 1 at 7:29





The problem has nothing to do with Laravel, that's why the link already answer your question. float is a data type that is characterized by its exponent and mantissa. So the output you get is actually the correct one which is also used internally by the PHP language (and every other language). What you want is a special human-readable version of it - and this is what you get by using sprintf() as shown in the linked question and answer.
– Namoshek
Jul 1 at 7:34


float


sprintf()





The cast you define in the model does only force a conversion between data types, i.e. it will make a float out of any other type when some input is passed for the corresponding property. This does ensure that your database does not fail when storing values. But it has nothing to do with the presentation of values.
– Namoshek
Jul 1 at 7:35


float





@Namoshek - any issue in Laravel has nothing to do with Laravel since it's all PHP, you can always simmer it down to that, yet if you notice that model mutating (as in both answers) has it's special function naming rules, paralleling or tying my OP to that issue you found in another stackoverflow question that represents a simple and terse PHP issue, is simply incorrect.
– erezt
Jul 1 at 7:56





2 Answers
2



What you seem to want is number formatting because 0.0000064 is the same as 6.4e-7 and you are only concerned about what the end user sees.



You can define an accessor to do this:


public function getTotalCryptoBalanceAttribute() {
return number_format($this->attributes['total_crypto_balance'], 8); // format with 8 decimals
}



apokryfos posted before me, so I marked him as the correct answer.



Yet only here to show another way:


public function getTotalCryptoBalanceAttribute($value)
{
return sprintf('%.8f', $value);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API