Convert number to scientific notation and obtain exponent flutter - 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());

Related

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.

How to represent large number without the E form in Scala

I deal with numbers of this form 1.446267186999E7 and i want to represent them without E.
For example 1.446267186999E7 i want it to be 14462671.86999 .
How do i convert it to this form without getting the :
error: integer number too large.
Thanks for the helpers.
Try this:
BigDecimal(1.446267186999E7).toString
The BigDecimal.toString method will give you the string representation of the number in decimal form.
That is just a formatting problem if you store it as a double.
import java.text.DecimalFormat
val d: Double = 1.446267186999E7
val decimalFormat: DecimalFormat = new DecimalFormat("0.#####")
println(decimalFormat.format(d))
should give you 14462671.86999
You probably after BigDecimal. In terms of string formatting, look at the .format method, or printf

Converting number in scientific notation to int

Could someone explain why I can not use int() to convert an integer number represented in string-scientific notation into a python int?
For example this does not work:
print int('1e1')
But this does:
print int(float('1e1'))
print int(1e1) # Works
Why does int not recognise the string as an integer? Surely its as simple as checking the sign of the exponent?
Behind the scenes a scientific number notation is always represented as a float internally. The reason is the varying number range as an integer only maps to a fixed value range, let's say 2^32 values. The scientific representation is similar to the floating representation with significant and exponent. Further details you can lookup in https://en.wikipedia.org/wiki/Floating_point.
You cannot cast a scientific number representation as string to integer directly.
print int(1e1) # Works
Works because 1e1 as a number is already a float.
>>> type(1e1)
<type 'float'>
Back to your question: We want to get an integer from float or scientific string. Details: https://docs.python.org/2/reference/lexical_analysis.html#integers
>>> int("13.37")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '13.37'
For float or scientific representations you have to use the intermediate step over float.
Very Simple Solution
print(int(float(1e1)))
Steps:-
1- First you convert Scientific value to float.
2- Convert that float value to int .
3- Great you are able to get finally int data type.
Enjoy.
Because in Python (at least in 2.x since I do not use Python 3.x), int() behaves differently on strings and numeric values. If you input a string, then python will try to parse it to base 10 int
int ("077")
>> 77
But if you input a valid numeric value, then python will interpret it according to its base and type and convert it to base 10 int. then python will first interperet 077 as base 8 and convert it to base 10 then int() will jsut display it.
int (077) # Leading 0 defines a base 8 number.
>> 63
077
>> 63
So, int('1e1') will try to parse 1e1 as a base 10 string and will throw ValueError. But 1e1 is a numeric value (mathematical expression):
1e1
>> 10.0
So int will handle it as a numeric value and handle it as though, converting it to float(10.0) and then parse it to int. So Python will first interpret 1e1 since it was a numric value and evaluate 10.0 and int() will convert it to integer.
So calling int() with a string value, you must be sure that string is a valid base 10 integer value.
int(float(1e+001)) will work.
Whereas like what others had mention 1e1 is already a float.

PARI:similar function like Integer.parseInt()

I want to convert the text hello to ascii decimal in PARI/GP. After that I will
concatenate the values.
I initialize a Vecsmall(hello), after that I run a loop to concatenate the ascii decimal values,
I want to use this concatenated value to * by certain values. The value is now in String type, In Java, there is a Integer.parseInt() to convert the string to int. I wonder if there is a similar function in PARI/GP?
v=Vecsmall("hello");'
for (i = 1, length(v), text=Str(text,v[i]););
//is there any similar function like Integer.praseInt(text) in PARI?
You can use eval
eval(text)
or else a combination of Vecsmall and fromdigits which is faster:
fromdigits(apply(n->n-49, Vec(text)))