Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:07:13+00:00 2026-05-30T18:07:13+00:00

Suppose that I have a string in scala and I want to try to

  • 0

Suppose that I have a string in scala and I want to try to parse a double out of it.

I know that, I can just call toDouble and then catch the java num format exception if this fails, but is there a cleaner way to do this? For example if there was a parseDouble function that returned Option[Double] this would qualify.

I don’t want to put this in my own code if it already exists in the standard library and I am just looking for it in the wrong place.

Thanks for any help you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:07:14+00:00Added an answer on May 30, 2026 at 6:07 pm

    For Scala 2.13+ see Xavier’s answer below. Apparently there’s a toDoubleOption method now.

    For older versions:

    def parseDouble(s: String) = try { Some(s.toDouble) } catch { case _ => None }
    

    Fancy version (edit: don’t do this except for amusement value; I was a callow youth years ago when I used to write such monstrosities):

    case class ParseOp[T](op: String => T)
    implicit val popDouble = ParseOp[Double](_.toDouble)
    implicit val popInt = ParseOp[Int](_.toInt)
    // etc.
    def parse[T: ParseOp](s: String) = try { Some(implicitly[ParseOp[T]].op(s)) } 
                                       catch {case _ => None}
    
    scala> parse[Double]("1.23")
    res13: Option[Double] = Some(1.23)
    
    scala> parse[Int]("1.23")
    res14: Option[Int] = None
    
    scala> parse[Int]("1")
    res15: Option[Int] = Some(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that you have a lengthy string (> 80 characters) that you want to
Suppose that I have the following string: http://www.domain.com/?s=some&two=20 How can I take off what
Suppose I have a string that contains Ü. How would I find all those
Suppose I have a simple class in Scala: class Simple { def doit(a: String):
Suppose I have the following scala classes ( that needs to improve to address
Suppose that I have a unsigned char*, let's call it: some_data unsigned char* some_data;
Suppose I have a List[Int] , and I want to call toString on each
Suppose that we have 2 classes A,B: class A extends SomeClass { public String
suppose that i have var name = kanishka; How can I cut only the
Suppose that I have a string $var : //php code $var = hello,world,test; $exp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.