What is a radix in integer class in dart programming? - class

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)'

Related

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.

Integer number too large error for hexadecimal value

For integers, I can use 'BigInt' if it throws a similar error, what about hexadecimal values? I strictly don't want to convert the hex value into an integer and then use it. Can somebody help?
val a = 0x1265465678687564534344536
<console>:1: error: integer number too large
That class BigInteger has a constructor that takes a String argument, so the simple answer is: instead of using a numeric literal, that is out of any meaningful range, create a BigInteger based on "0x1265465678687564534344536" instead!
Use the BigInteger class that takes in a string parameter and pass the radix as 16 to indicate hexadecimal string.
new BigInteger(string, 16)

How to convert from Octal to Decimal in Swift

Hi I am trying to convert an octal number to decimal in swift. What would be the easiest way to do this?
From Octal to Decimal
There is a specific Int initializer for this
let octal = 10
if let decimal = Int(String(octal), radix: 8) {
print(decimal) // 8
}
From Decimal to Octal
let decimal = 8
if let octal = Int(String(decimal, radix: 8)) {
print(octal) // 10
}
Note 1: Please pay attention: parenthesis are different in the 2 code snippets.
Note 2: Int initializer can fail for string representations of number with more exotic radixes. Please read the comment by #AMomchilov below.
you can convert from octal to decimal easily. Swift supports octal syntax natively. You have to write "0o" before the octal number.
let number = 0o10
print(number) // it prints the number 8 in decimal
Integer Literals
Integer literals represent integer values of unspecified precision. By
default, integer literals are expressed in decimal; you can specify an
alternate base using a prefix. Binary literals begin with 0b, octal
literals begin with 0o, and hexadecimal literals begin with 0x.
Here is the documentation's reference.
I hope it helps you

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.

Objective-C Decimal to Base 16 Hex conversion

Does anyone have a code snippet or a class that will take a long long and turn it into a 16 byte Hex string?
I'm looking to turn data like this
long long decimalRepresentation = 1719886131591410351;
and turn it into this
//Base 16 Hex Output: 17DE435307A07300
The %x operator doesn't want to work for me
NSLog(#"Hex: %x",decimalRepresentation);
//console : "Hex: 7a072af"
As you can see that's not even close. Any help is truly appreciated!
%x prints an unsigned integer in hexadecimal representation and sizeof(long long) != sizeof(unsigned). See e.g. "Data Type Size and Alignment" in the 64bit transitioning guide.
Use the ll specifier (thats two lower-case L) to get the desired output:
NSLog(#"%llx", myLongLong);