for example +100286020524,17 how to become Rp 100.286.020.524,17 and remove this sign + . how to implement in dart flutter
EDIT
this mycode
I using indonesia: 1.0.1 packages
main() {
rupiah(123456789); // Rp 123,456,789
}
I've tried like this:
replaceAll(new RegExp(r'[^\w\s]+'),'')
but not i hope output.
how to remove symbol plus
To easily format values according to a locale it is recommended to use intl.
A currency can be formatted like this for Indonesia:
double d = 100286020524.17;
final currencyFormatter = NumberFormat.currency(locale: 'ID');
print(currencyFormatter.format(d)); // IDR100.286.020.524,17
You can also use classic formatting:
double d = 100286020524.17;
final currencyFormatter = NumberFormat('#,##0.00', 'ID');
print(currencyFormatter.format(d)); // 100.286.020.524,17
In this second way you will have only the value formatted without the currency symbol.
Related
For interaction with an API, I need to pass the course code in <string><space><number> format. For example, MCTE 2333, CCUB 3621, BTE 1021.
Yes, the text part can be 3 or 4 letters.
Most users enter the code without the space, eg: MCTE2333. But that causes error to the API. So how can I add a space between string and numbers so that it follows the correct format.
You can achieve the desired behaviour by using regular expressions:
void main() {
String a = "MCTE2333";
String aStr = a.replaceAll(RegExp(r'[^0-9]'), ''); //extract the number
String bStr = a.replaceAll(RegExp(r'[^A-Za-z]'), ''); //extract the character
print("$bStr $aStr"); //MCTE 2333
}
Note: This will produce the same result, regardless of how many whitespaces your user enters between the characters and numbers.
Try this.You have to give two texfields. One is for name i.e; MCTE and one is for numbers i.e; 1021. (for this textfield you have to change keyboard type only number).
After that you can join those string with space between them and send to your DB.
It's just like hack but it will work.
Scrolling down the course codes list, I noticed some unusual formatting.
Example: TQB 1001E, TQB 1001E etc. (With extra letter at the end)
So, this special format doesn't work with #Jahidul Islam's answer. However, inspired by his answer, I manage to come up with this logic:
var code = "TQB2001M";
var i = course.indexOf(RegExp(r'[^A-Za-z]')); // get the index
var j = course.substring(0, i); // extract the first half
var k = course.substring(i).trim(); // extract the others
var formatted = '$j $k'.toUpperCase(); // combine & capitalize
print(formatted); // TQB 1011M
Works with other formats too. Check out the DartPad here.
Here is the entire logic you need (also works for multiple whitespaces!):
void main() {
String courseCode= "MMM 111";
String parsedCourseCode = "";
if (courseCode.contains(" ")) {
final ensureSingleWhitespace = RegExp(r"(?! )\s+| \s+");
parsedCourseCode = courseCode.split(ensureSingleWhitespace).join(" ");
} else {
final r1 = RegExp(r'[0-9]', caseSensitive: false);
final r2 = RegExp(r'[a-z]', caseSensitive: false);
final letters = courseCode.split(r1);
final numbers = courseCode.split(r2);
parsedCourseCode = "${letters[0].trim()} ${numbers.last}";
}
print(parsedCourseCode);
}
Play around with the input value (courseCode) to test it - also use dart pad if you want. You just have to add this logic to your input value, before submitting / handling the input form of your user :)
I am still looking for a better way to localize a percentage value in Dart/Flutter. So far, I'm just converting a percentage value to a suitable string with the following code: '${(_percentage * 100).toStringAsFixed(3)}%'
For a percentage value of e.g. 65.7893 you get the following string: 65.789%. That's fine for English, but not for other languages.
For example, for German, the string should be as follows: 65,789 %
Solution
Thanks to the useful tips, I have now created the following wrapper function.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
_generatePercentString(percentValue) {
var systemLocale = Localizations.localeOf(context);
var numberFormatter = NumberFormat.decimalPercentPattern(
locale: systemLocale.languageCode,
decimalDigits: 3,
);
var percentString = numberFormatter.format(percentValue);
return percentString;
}
First add intl package into your pubspec.yaml
Use the Localisation and NumberFormat class.
Locale myLocale = Localizations.localeOf(context); /* gets the locale based on system language*/
String languageCode = myLocale.languageCode;
print(NumberFormat.percentPattern(languageCode).format (60.23));
You can refer here for the the supported locales :
https://www.woolha.com/tutorials/dart-formatting-currency-with-numberformat#supported-locales
I have read many questions on stackoverflow and didn't get what I want. I have a String with value: "1.000,00" and I want to make it a double and look exactly like this "1000.0" but I can't do it right. Using the code below, I just get "1.0" and this is incorrect. How to do this in the right way? I am using Flutter.
String t = "1.000,00";
double f = NumberFormat().parse(t);
userIncome = f;
You just need to set the defaultLocale property.
Intl.defaultLocale = 'pt_BR';
String t = "1.000,00";
double f = NumberFormat().parse(t);
print(f); // Prints 1000.0
In your case, is not just a "number", is a currency.
A simple way to deal with it is to convert into a number and parse:
String t = "1.000,00";
// the order of replaces here is important
String formated = t.replace(".", "").replace(",", ".");
userIncome = Double.valueOf(formated);
But, if your application is focus on currency you should take a look in this:
https://www.baeldung.com/java-money-and-currency
I want to replace part of the string with asterisk (* sign).
How can I achieve that? Been searching around but I can't find a solution for it.
For example, I getting 0123456789 from backend, but I want to display it as ******6789 only.
Please advise.
Many thanks.
Try this:
void main(List<String> arguments) {
String test = "0123456789";
int numSpace = 6;
String result = test.replaceRange(0, numSpace, '*' * numSpace);
print("original: ${test} replaced: ${result}");
}
Notice in dart the multiply operator can be used against string, which basically just creates N version of the string. So in the example, we are padding the string 6 times with'*'.
Output:
original: 0123456789 replaced: ******6789
try using replaceRange. It works like magic, no need for regex. its replaces your range of values with a string of your choice.
//for example
prefixMomoNum = prefs.getString("0267268224");
prefixMomoNum = prefixMomoNum.replaceRange(3, 6, "****");
//Output 026****8224
You can easily achieve it with a RegExp that matches all characters but the last n char.
Example:
void main() {
String number = "123456789";
String secure = number.replaceAll(RegExp(r'.(?=.{4})'),'*'); // here n=4
print(secure);
}
Output: *****6789
Hope that helps!
How can I determine the symbol for the decimal separator? The device's current locale should be used.
You need to use methods of NumberFormat class, available in intl library.
NumberFormat.decimalPattern formats a number according to specified locale.
e.g. this is how you can get device locale and format a number.
import 'package:intl/intl.dart' show NumberFormat;
import 'package:intl/number_symbols_data.dart' show numberFormatSymbols;
import 'dart:ui' as ui;
String getCurrentLocale() {
final locale = ui.window.locale;
final joined = "${locale.languageCode}_${locale.countryCode}";
if (numberFormatSymbols.keys.contains(joined)) {
return joined;
}
return locale.languageCode;
}
// ...
String dec = NumberFormat.decimalPattern(getCurrentLocale()).format(1234.567);
print(dec); // prints 1,234.567 for `en_US` locale
However, to answer your initial question - here is a function that returns either decimal separator (if found any) or empty string.
String getDecimalSeparator(String locale) {
return numberFormatSymbols[locale]?.DECIMAL_SEP ?? "";
}
// ...
print(getDecimalSeparator(getCurrentLocale())); // prints `.` for `en_US` locale.
Let me know if this helped.
NumberFormat only uses language code to format numbers which is technically correct. However in iOS from settings users can set number format preferences for some languages like English. This can cause a problem for your textfield because decimal separator can be different on the number keyboard. I developed a package to fix that issue. You can use locale_plus to get decimal and grouping separator as set in device settings. After that you can set the locale of the NumberFormat according to separator characters you got from the plugin.