Skip to content

Instantly share code, notes, and snippets.

@VB10
Created May 17, 2024 11:18
Show Gist options
  • Save VB10/a1f7a22c8ad60e7b7d72f402dd193c6b to your computer and use it in GitHub Desktop.
Save VB10/a1f7a22c8ad60e7b7d72f402dd193c6b to your computer and use it in GitHub Desktop.
Take a count of digit in any number
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