Rumahjo-Android-APP/lib/utils/string_extenstion.dart

52 lines
1.4 KiB
Dart
Raw Normal View History

2024-09-07 00:58:50 +00:00
import 'package:intl/intl.dart';
String formatNumberWithSuffix(int number) {
if (number < 1000) {
return number.toString();
} else if (number < 1000000) {
return '${(number / 1000).toStringAsFixed(0)} ribu';
} else if (number < 1000000000) {
return '${(number / 1000000).toStringAsFixed(0)} juta';
} else if (number < 1000000000000) {
return '${(number / 1000000000).toStringAsFixed(0)} milyar';
} else {
return '${(number / 1000000000000).toStringAsFixed(0)} triliun';
}
}
extension StringExtention<T extends String> on T {
// T firstUpperCase() {
// String upperCase = "";
// var suffix = "";
// if (isNotEmpty) {
// upperCase = this[0].toUpperCase();
// suffix = substring(1, length);
// }
// return (upperCase + suffix) as T;
// }
///Number with suffix 10k,10M ,1b
String priceFormate({bool? disabled, bool? isCurrency}) {
double numericValue = double.parse(this);
String formattedNumber = '';
if (isCurrency == true) {
/// If the numeric value is an integer, show it without decimal places
formattedNumber = formatNumberWithSuffix(numericValue.toInt());
} else {
// If the numeric value has decimal places, format it with 2 decimal digits
formattedNumber = NumberFormat('#,##0.00').format(numericValue);
}
if (disabled == true) {
return this;
}
return formattedNumber;
}
double toDouble() {
return double.parse(this);
}
}