Parse and format a number to octal in Java


To parse and format a number to octal, try the following code −

Example

 Live Demo

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

104
150

In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to octal.

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

The toString() method above represents a value in string and formats.

We have set the radix as 8, since octal is represented by radix 8.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

451 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements