Created
May 17, 2024 11:18
-
-
Save VB10/a1f7a22c8ad60e7b7d72f402dd193c6b to your computer and use it in GitHub Desktop.
Take a count of digit in any number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NumberExtension on num { | |
/// This method is used to take the first [count] digits of the number. | |
/// | |
/// if the number is 59342394234 and [count] is 2, the result will be 59. | |
/// number less than [count] will return 0. | |
num takeDigits(int count) { | |
final value = toString(); | |
final isValueLengthValid = value.length >= count; | |
if (!isValueLengthValid) return 0; | |
return num.parse(value.substring(0, count)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment