Open RomainLanz opened 4 years ago
Couple of questions.
You proposed to use Regex for matching the decimal places. But what will be the value after casting the value? For example:
'22.00'
.Number('22.00')
22
and not 22.00
.positive/negative
over signed/unsigned
Don't you think developers are very much aware of signed/unsigned
values? Databases use them and even Javascript own API uses Math.sign
to know if the value is signed or unsigned
signed/unsigned does not usually mean the same as positive/negative. In a database, a signed value can be positive (when the sign bit is off).
@targos Can you link me to some doc explaining this a bit in depth? Really interested to know the difference between them 😄
You proposed to use Regex for matching the decimal places. But what will be the value after casting the value?
The rule will verify that the number of decimal don't go over the given number.
Using your example, the rule would pass because 22
is valid since it doesn't have more than 2 decimals.
In fact, 22
and 22.00
are exactly the same number for the JavaScript engine.
22 === 22.00 // true
Don't you think developers are very much aware of signed/unsigned values? Databases use them and even Javascript own API uses Math.sign to know if the value is signed or unsigned
As @targos said, a signed
number can be both positive and negative (we know it's one or the other because of the signed bit), unsigned
means it's always positive because there's no signed bit to determine if the number can be something else.
Here's the Wikipedia article on signed bit and unsigned integer
In fact, 22 and 22.00 are exactly the same number for the JavaScript engine.
What's the use case for validating decimal places when the casted value can be different. I am struggling to see when I will say I want 22.00
and 22
As @targos said, a signed number can be both positive and negative (we know it's one or the other because of the signed bit), unsigned means it's always positive because there's no signed bit to determine if the number can be something else.
I get it now. So yes, having a positive or negative seems more helpful than having signed
or unsigned
.
In fact, 22 and 22.00 are exactly the same number for the JavaScript engine.
What's the use case for validating decimal places when the casted value can be different. I am struggling to see when I will say I want
22.00
and22
This rule will be useful when people need to do math with the received number.
Let's say you don't want to store decimal number inside your database, therefore you're doing something like xxx * 100
to convert a two decimals number to an integer.
22.00 * 100 // 2200
22 * 100 // 2200
22.22 * 100 // 2222
22.222 * 100 // 2222.2(000000000003)
If we have more than two decimals, our code will fail. There are two ways to avoid this failure:
Math.floor()
or Math.ceil()
beforeWas planning to start the work on this. Do you guys think, following is the right representation
22.00
22
-1
-1.00
22.10
.3
22.10
-22.10
.3
22.00
-22.00
-22
22.10
.3
-22.10
22.00
-22.00
-22
22
positive
and negative
can be used with decimal
, float
and integer
What would be the use case for isFloat? I can't think of a case where you absolutely want something after the comma that is not zero
What would be the use case for isFloat? I can't think of a case where you absolutely want something after the comma that is not zero
My personal use cases with numbers are very minimal, so I don't have to good answer for that. I added this to handle another use case (which is not personally aware of)
Also, we use this library in AdonisJS validator and they have two different methods called isDecimal
and isFloat
. So that was also an inspiration.
Rendered
Introducing new rules for number validation.
integer
decimal
positive
negative