Methods to introduce
The floor method in Lodash is similar to math.floor, but it takes two arguments: _. Floor (number,[precision=0])
- “Number” is the value to trade off
- Precision is the precision of the downward trade-offs, which can be interpreted as reserving a few decimal places, as distinguished from math.floor
The source code to explain
import createRound from './.internal/createRound.js'
/ * * * * floor (4.006) * / / = > 4 * * floor (0.046, 2) * * * / / = > 0.04 floor (4060-2) * * / / / = > 4000
const floor = createRound('floor')
export default floor
Copy the code
CreateRound is a function called createRound.
/**
* Creates a function like `round`.
*
* @private
* @param {string} methodName The name of the `Math` method to use when rounding.
* @returns {Function} Returns the new round function.
*/
function createRound(methodName) {
const func = Math[methodName]
return (number, precision) = > {
precision = precision == null ? 0 : (precision >= 0 ? Math.min(precision, 292) : Math.max(precision, -292))
if (precision) {
let pair = `${number}e`.split('e')
const value = func(`${pair[0]}e${+pair[1] + precision}`)
pair = `${value}e`.split('e')
return +`${pair[0]}e${+pair[1] - precision}`
}
return func(number)
}
}
export default createRound
Copy the code
Inside the createRound function, a function is returned, which is the body of the Floor method of LoDash
- Func = Math[methodName], we passed in floor, so func is math.floor
- Precision == null, and the double equal sign is not strictly equal, so the precision may be undefiend and NULL. In this case, the value of precision is 0, and the value of precision is between -292-292
- If precision is 0, return func(number), math.floor (number)
If the precision value is passed in, it goes inside the condition judgment
- Define an array (let pair =
${number}e
.split(‘e’)), the value is [number, “], but the number type is a string
const number = 4.006;
`${number}e`.split('e') / / / '4.006' ' '
Copy the code
- Call the func(math.floor) method,
${pair[0]}e${+pair[1] + precision}
For example, the number passed in is 4.006, precision is 2, ‘4.006e2’, scientific notation is 4.006*10^2, The resulting value is an integer magnified by 10 to the precision (in this case, 400).
${pair[0]}e${+pair[1] + precision} / / e2 = = = 400.6 4.006
const value = Math.floor(4.006 e2) / / 400
Copy the code
${value}e
.split(‘e’); split(‘e’); split(‘e’); split(‘e’); split(‘e’)
pair = `${value}e`.split('e') // '400e'.split('e') == ['400','']
Copy the code
- Finally, return +
${pair[0]}e${+pair[1] - precision}
In the string template, it’s just like the string template above to get value, except that it’s the string template above+precision
And here is the-precision
That is, if I multiply this by 10^2, then I’m dividing this by 10^2, which is if I zoom in, I’m going to have to zoom outprecision
If it’s a negative number, I’m going to zoom in, so I’m going to go back to number; The + is an implicit type conversion that converts a string to a number
`${pair[0]}e${+pair[1] - precision}` //'400e-2'
+'400e-2' //400e-2 === 4
Copy the code
Here’s a question:
- Why is it that each time you make an array out of number or value, you take the index of the array in the next string concatenation
Array and subscript, or ${number} instead of pair[0], is the same, there is no difference, should be a personal problem