Convert scientific notation to number ( dart & flutter ) - flutter

how can I convert scientific notation to number in dart ?
ex: 9.565108299996563e-10 to 0.0000000009565108299996563
9.565108299996563e-10 to 0.0000000009565108299996563

String scientificNotation = "9.565108299996563e-10";
double number = double.parse(scientificNotation);
String formattedNumber = number.toStringAsFixed(20);
print(formattedNumber); // 0.00000000095651083000

Related

Convert number to scientific notation and obtain exponent flutter

How can I convert a number to scientific notation and obtain the exponent? For example if I have 0.000000001 and want to convert it to 1e-9
Use the intl package and try it as following
final value = 0.000000025;
String res = NumberFormat.scientificPattern(Localizations.localeOf(context).languageCode).format(value);
print("Formatted value is $res");
EDIT
A better and cleaner approach, which does not require any additional package, is the method toStringAsExponential(). Here is how to use it
final value = 0.000000025;
print(value.toStringAsExponential());

What is a radix in integer class in dart programming?

I am learning dart programming language for Flutter. In the integer class what does the word radix means ? Please explain me this. Thanks
Sometimes we have to work with string in radix number format. Dart int parse() method also supports convert string into a number with radix in the range 2..36:
For example, we convert a Hex string into int:
var n_16 = int.parse('FF', radix: 16);
The output of the code = 255
Using radix function we can also convert Binary numbers into decimal numbers like this
var decimal = int.parse('1001001', radix:2)'

Counterintuitive result in NumberFormat of Intl Package in Dart/Flutter

Why does NumberFormat(".##").format(17.46) leads to a string of 17.46 and not .46?
How can I achieve the latter, i.e. remove all digits in front of the decimal sign?
The NumberFormat only changes the way that a number is being displayed(basically, what formatting is). So you can't get the fractional part of the number(it doesn't work like pattern matching).
Instead, you can use:
var num = 17.46;
var fraction = num.toString().split('.')[1];
Note: you can use '.' + num.toString().split('.')[1] to get the fraction part with the starting dot.
You can read more about the ICU Formatting that NumberFormat uses in this link.
Just as an alternative to the other answer, you can try to remove the integer part before converting to String, and not after:
String formatFraction (num a){
num b = a.floor();
num c = a-b;
return NumberFormat(".##").format(c);
}
This way you can guarantee it will work despite of locale.
‘#’ in the NumberFormat class marks a single digit (omitted if the value is zero). So the number of hashtags after the decimal point denotes how many decimal places you want. For example:
double number = 12.1234;
NumberFormat(".#").format(number); //prints 12.1
NumberFormat(".##").format(number); //prints 12.12
NumberFormat(".###").format(number); //prints 12.123
NumberFormat(".####").format(number); //prints 12.1234
You could use substring and indexOf to remove everything before the decimal point, like so:
String str = "12.36";
String newStr = str.substring(str.indexOf('.') + 1);
//If you want to include the decimal point, remove the + 1.

Format String containing decimal values to Number using Scala

Format String containing decimal values to Number using Scala
For example if var a="34.523" after formatting should be "34523".
Replace . with empty string?
scala> "12.343".replaceAll("\\.", "")
res0: String = 12343
There is also:
a.replace(".","")

GWT get users local decimal separator char

How can I get users local decimal separator character from GWT. I have found the NumberFormat utility, which only returns the whole format and provides parsing.
NumberFormat.getDecimalFormat()...
Is there a (simple) way to get the decimal separator, thousand separator from the decimal format.
import com.google.gwt.i18n.client.LocaleInfo;
String decimalSeparator = LocaleInfo.getCurrentLocale().getNumberConstants().decimalSeparator();
String thousandSeparator = LocaleInfo.getCurrentLocale().getNumberConstants().groupingSeparator();
You can seed it with a known number and then parse it for the characters you want.
import com.google.gwt.i18n.client.NumberFormat;
NumberFormat fmt = NumberFormat.getDecimalFormat();
double value = 1234.5;
String formatted = fmt.format(value);
String thousandSeparator = formatted.substring(1,2);
String decimalSeparator = formatted.substring(5,6);