English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Parse a Number and Format as Octal in Java

To parse and format a number into octal, try the following code-

Example

public class Demo {
   public static void main(String args[]) {
      int val = Integer.parseInt("150", 8);
      System.out.println(val);
      String str = Integer.toString(val, 8);
      System.out.println(str);
   }
}

Output Result

104
150

In the above program, we used Integer.parseInt() and Integer.toString() methods to convert numbers to octal.

int val = Integer.parseInt("150", 8);
String str = Integer.toString(val, 8);

toString()The above method represents a value as a string and format.

We set the base to8because octal uses a base8Representation.