Skip to content

Commit

Permalink
ready_extensions_1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed dawood committed Apr 18, 2022
1 parent 73a5e56 commit a915473
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/ready_extensions/extensions/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.1"
version: "1.0.2"
ready_extensions_dart:
dependency: transitive
description:
name: ready_extensions_dart
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion packages/ready_extensions/extensions/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ packages:
name: ready_extensions_dart
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down
4 changes: 2 additions & 2 deletions packages/ready_extensions/extensions/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ready_extensions
description: Collection of extension methods to make your code simple , clean and fast
version: 1.0.1
version: 1.0.2
homepage: https://github.com/mo-ah-dawood/ready/tree/main/packages/ready_extensions
repository: https://github.com/mo-ah-dawood/ready
issue_tracker: https://github.com/mo-ah-dawood/ready/issues
Expand All @@ -13,7 +13,7 @@ dependencies:
flutter:
sdk: flutter
intl: ^0.17.0
ready_extensions_dart: ^1.0.0
ready_extensions_dart: ^1.0.2

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import 'package:intl/intl.dart';

extension NumExtensions on num? {
/// format the current number using NumberFormat
String format(NumberFormat format) {
/// ex. #.0#
String format(String _format) {
if (this == null) return '';
return formatWith(NumberFormat(_format));
}

/// format the current number using NumberFormat
String formatWith(NumberFormat format) {
if (this == null) return '';
return format.format(this);
}
Expand All @@ -13,22 +20,19 @@ extension NumExtensions on num? {
/// 15.5 => 15.50
/// 15.500 => 15.50
/// 15.00 => 15.00
String noTrailing([int? fractionDigits]) {
String noTrailing({
int? fractionDigits,
bool grouping = true,
String? locale,
}) {
if (this == null) return '';
var str = ((fractionDigits == null
? this!.toString()
: this!.toStringAsFixed(fractionDigits)))
.replaceAll(r'\.0+$', '');

var dotIndex = str.indexOf(".");
if (dotIndex < 0 || fractionDigits != null) return str;
var splits = str.split('.');
var trailing = splits[1].replaceAll(RegExp(r'0+$'), '');
if (trailing.isEmpty) {
return splits[0];
var _format = grouping ? '#,##0.' : '#.';
if (fractionDigits == null) {
_format += '#############';
} else {
return '${splits[0]}.$trailing';
_format += List.filled(fractionDigits, '0').join();
}
return formatWith(NumberFormat(_format, locale));
}

/// format number with intl currency format
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ready_extensions_dart
description: Collection of extension methods to make your code simple , clean and fast
version: 1.0.0
version: 1.0.2
homepage: https://github.com/mo-ah-dawood/ready/tree/main/packages/ready_extensions_dart
repository: https://github.com/mo-ah-dawood/ready
issue_tracker: https://github.com/mo-ah-dawood/ready/issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ void main() {
expect((15.5).noTrailing(), '15.5');
expect((15.55).noTrailing(), '15.55');
expect((15.500005).noTrailing(), '15.500005');
expect((15.5000050).noTrailing(10), '15.5000050000');
expect((15.5000050).noTrailing(fractionDigits: 10), '15.5000050000');
expect((15.50000500000).noTrailing(), '15.500005');
expect((15.5000050).noTrailing(), '15.500005');
expect((0.5000050).noTrailing(), '0.500005');
expect((15.5).noTrailing(2), '15.50');
expect((15.00).noTrailing(2), '15.00');
expect((15.05).noTrailing(2), '15.05');
expect((15.5).noTrailing(fractionDigits: 2), '15.50');
expect((15.00).noTrailing(fractionDigits: 2), '15.00');
expect((15.05).noTrailing(fractionDigits: 2), '15.05');
expect(15.5.noTrailing(), '15.5');
expect((30000.0).noTrailing(), '30000');
expect((30000.0).noTrailing(), '30,000');
expect((30000.0).noTrailing(grouping: false), '30000');
});

test('iterable extensions', () {
Expand Down

0 comments on commit a915473

Please sign in to comment.