How to convert decimal to/from an Octal number in Dart
Convert Octal to Decimal in Dart:
- using
int.parseInt(String, {radix: base})
method, base as 16 .int.parse('00702', radix: 8);
returnsoctal
value450
.
- using
Convert Decimal to Octal in Dart:
- use
Number.toRadixString(8)
method, For example:450.toRadixString(8)
returnsoctal
value702
.
- use
An Octal Number
is a number with eight digits. The Base 8 numbering system, often known as Octal numerals, is used.
Octal numerals in Dart contain decimal numbers (0-9) such as 0,1,2,3,4,5,6,7. 42 or 3,3 are examples of Octal numbers.
Decimal Numbers
are numbers having a base of 10, or numbers spanning from 0 to 9.
45 is an example of a decimal number. Learn how to convert decimal to Octal in a dart in this quick tutorial.
How to convert decimal numbers to Octal numbers in Dart?
In this example, in dart and flutter, a decimal number with base 10 is converted to an Octal number with base 8.
In Dart, decimal numbers are represented using the int
type. It provides a toRadixString()
method that accepts a base parameter.
Number.toRadixString(base);
the base can be 16 for Hexa
, 8 for octal
,2 for binary
.
Here is a code for parsing decimal to Octal example.
void main() {
final decimalNumber = 450;
final octalNumber = decimalNumber.toRadixString(8); // 702
print(octalNumber);
final fiveDigitOctalNumber = octalNumber.padLeft(5, '0');
print(fiveDigitOctalNumber);
}
Output:
702
00702
use the padLeft
method to display the octal number limit to 5 digits.
final fiveDigitOctalNumber = octalNumber.padLeft(5, '0');
It appends left padding with zeroes.
How to parse Octal numbers to decimal numbers in Dart?
This example converts Octal to decimal numbers in dart and flutter.
The int
has the parseInt
method which takes a string number and bases it on 8.
syntax
int.parseInt(String, {radix: base})
The string is a string of Octal numbers to convert. the base is a base such as 2, 8,16, etc.
Here is a code for convert Octal to decimal example
void main() {
final decimalNumber = 450;
final octalNumber = decimalNumber.toRadixString(8); // 9e
print(octalNumber);
final fiveDigitOctalNumber = octalNumber.padLeft(5, '0');
print(fiveDigitOctalNumber);
int decimalNumber1 = int.parseInt('00702', radix: 8);
print(decimalNumber1);
}
Output:
702
00702
450
Conclusion
In a summary, Learned the following examples
- Convert decimal to Octal number using the
toRadixString
method - Convert Octal to decimal number using
parseInt()
method