Laravel - Number with 4 decimals
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Storing Numbers with Decimals in Laravel Databases: A Comprehensive Solution
Introduction: Laravel offers various data types for handling numeric values in your database tables, but sometimes you need to store numbers with decimals more specific than the default options allow. In this blog post, we'll discuss how to achieve this using proper data types and their configurations to ensure accurate data persistence and efficient storage.
1. Using Float: Let's start with the float. This data type can be used for storing numbers with decimals but is limited to only 2 decimal places. To enable floating-point values in your database table, use the following syntax while defining a column:
$table->float('sell');
This code snippet defines a sell column with a float type which allows up to two decimals, but doesn't let you specify the precision or scale.
2. Using Decimal: Laravel also provides a dedicated data type decimal specifically for handling numbers with decimals. However, using it requires careful consideration of its internal workings. The first argument refers to the total number of digits (M) allowed in the decimal portion, while the second one specifies how many digits are permitted after the decimal point (D). To create a decimal column for your sell data with 4 decimals, use this code:
$table->decimal('sell', 1, 4);
Unfortunately, as you noticed, this throws an error stating that the first number must be greater than or equal to the second number. This is due to Laravel's validation process which ensures that M >= D for all decimal columns in your database table.
3. Solving the Issue: To fix this issue, one solution is to use a custom type. The data types defined above will be considered as user-defined custom types by default if their parameters (M and D) don't follow Laravel's conventions. So, you can create your own custom type for handling decimal values with specific precision and scale:
$table->custom('sell', 'newDecimal(1, 4)');
Here, the newDecimal() function is a user-defined type definition that sets the total number of digits (M) to 1 (since this is for a sell column) and the precision after the decimal point (D) to 4. While not as convenient as using built-in data types, this approach ensures that your table's structure remains compatible with Laravel's database layer.
4. Optimizing Storage: Lastly, when designing your database tables, it is essential to choose appropriate data types according to the nature of the data being stored. In this case, if you know that you'll never store numbers with more than four places after the decimal point for sell values, consider using a decimal type with M = 1 and D = 4. This will ensure storage efficiency while still allowing you to handle all the decimals required in your application.
Conclusion: To correctly store numbers with decimals in Laravel databases, use either the float or decimal data types as per your requirements. Floats allow up to two decimal places, whereas decimals provide greater flexibility but require precise specification of total digits and digit precision. For optimum efficiency, use a custom type if necessary or choose sensible values for M and D in your application. By following these guidelines, you can ensure accurate data persistence and efficient storage while maintaining database table integrity.