{

How to convert decimal to/from an Octal number in Dart


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 an 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); // 9e
  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 present 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.parse('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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example