String to Number Conversion in Julia

Julia is a flexible, dynamic, and high-level programming language that can be used to write any application. Also, many of its features can be used for numerical analysis and computational science. Julia is being widely used in machine learning, visualization, and data science. Julia allows type conversion for compatible datatypes. Julia has an inbuilt parse method that allows conversion of string to numeric datatype. 

A string can be converted to the desired numeric datatype until it’s an invalid string. We can also specify the base for conversion like decimal, binary, octal or hexadecimal. 

Syntax: parse(T::Type, str, base=Int)

Parameters: 
Type: Specifies the datatype to which the String is to be converted. 
str: It is the String that is to be converted to the specified datatype. 
base: This is optional. Required only if the String must be converted to a Number of specific bases. 

Following are the ways for conversion from String to Number in Julia: 



Converting String Input to Number 

The string input is taken from the user using the readline() method of Julia. Next, the parse() method is used to convert the String into Integer datatype. The typeof() method outputs the datatype of the resulting integer. 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# reads the string 
a = readline() 
  
# prints the string 
println(a) 
  
# parsing the string to integer 
print(typeof(parse(Int64, a)))

chevron_right


Output :

Converting String to a Number with some base

The string input is taken from the user using the readline() method of Julia. Next, the parse() method is used to convert the String into Integer datatype with the given base(here octal). The typeof() method outputs the datatype of the resulting integer. 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Taking User Input
println("Enter the number"
a = readline() 
println("Entered the number: "a) 
  
# 8 is the octal base 
println(parse(Int64, a, 8)) 
  
# Getting Type of data
print(typeof(parse(Int64, a, 8)))

chevron_right


Output :



Taking String Input and base from the user and converting it into Number 

The string input and required base are taken from the user using the readline() method of Julia. The parse() method converts the base into Integer(Int64). Next, the parse() method is used to convert the String into Integer datatype with the given base(here hexadecimal). The typeof() method outputs the datatype of the resulting integer. 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Taking String from user
println("Enter the number"
a = readline() 
println("Entered the number: "a) 
  
# Taking base from user
println("Enter the base(10,8,16,2)"
base = readline() 
println("Entered base: "base) 
  
# Converting to Number
println(parse(Int64, a, parse(Int64,base))) 
print(typeof(parse(Int64, a, parse(Int64,base))))

chevron_right


Output :

Convert String to Float64 

The string input is taken from the user using the readline() method of Julia. Next, the parse() method is used to convert the String into Float datatype. The typeof() method outputs the datatype of the resulting float value. 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# parsing string into float 
println(parse(Float64, "123.345")) 
print(typeof(parse(Float64, "123.345")))

chevron_right


Output :

Invalid String throws an exception 

Since the given string cannot be converted into Float type the parse() method throws an error. 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# error will be thrown as string
# is not a valid float representation 
print(parse(Float64, "123.345a"))

chevron_right


Output :




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.