scala - restricted way of converting string to number
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a string variable that needs to be parsed to a double
Code:
val myString = "9d"
myString.toDouble // produces "9.0" which i don't want. this should throws an error
How do I make my string to be interpreted literally so that it produces an exception?
java scala number-formatting
add a comment |
I have a string variable that needs to be parsed to a double
Code:
val myString = "9d"
myString.toDouble // produces "9.0" which i don't want. this should throws an error
How do I make my string to be interpreted literally so that it produces an exception?
java scala number-formatting
Interesting question! This applies to pure Java too. One solution:BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01
add a comment |
I have a string variable that needs to be parsed to a double
Code:
val myString = "9d"
myString.toDouble // produces "9.0" which i don't want. this should throws an error
How do I make my string to be interpreted literally so that it produces an exception?
java scala number-formatting
I have a string variable that needs to be parsed to a double
Code:
val myString = "9d"
myString.toDouble // produces "9.0" which i don't want. this should throws an error
How do I make my string to be interpreted literally so that it produces an exception?
java scala number-formatting
java scala number-formatting
edited Nov 22 '18 at 13:50
ygor
1,1521616
1,1521616
asked Nov 22 '18 at 10:24
Vincent ClaesVincent Claes
442723
442723
Interesting question! This applies to pure Java too. One solution:BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01
add a comment |
Interesting question! This applies to pure Java too. One solution:BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01
Interesting question! This applies to pure Java too. One solution:
BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01
Interesting question! This applies to pure Java too. One solution:
BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01
add a comment |
2 Answers
2
active
oldest
votes
So, apparently, Java's java.lang.Double#parseDouble
is able to parse all kinds of strings, e.g. NaN
, Infinity
and other.
The answer to your problem seems to be BigDecimal(myString).toDouble
Out of curiosity, here is, what three different approaches return for various kinds of input strings:
def main(args: Array[String]): Unit = {
val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
val parsers = Seq(
("Double", (s: String) => s.toDouble),
("BigDecimal", (s: String) => BigDecimal(s).toDouble),
("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
)
for (string <- strings) {
println(s"n------------- $string ------------")
for ((name, parser) <- parsers) {
val result = Try(parser(string)) match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => ex.toString
}
println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
}
}
}
Result:
------------- Infinity ------------
Double -> Infinity
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"
------------- NaN ------------
Double -> NaN
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "NaN"
------------- 9.0d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9f ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9.0 ------------
Double -> 9.0
BigDecimal -> 9.0
NumberFormat -> 9.0
------------- 9.1 ------------
Double -> 9.1
BigDecimal -> 9.1
NumberFormat -> 9.1
------------- 1.4e14 ------------
Double -> 1.4E14
BigDecimal -> 1.4E14
NumberFormat -> 1.4
1
The second one doesn't work as desired:val n = NumberFormat.getNumberInstance.parse("9d")
results inn: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
add a comment |
I found a solution with scalaz
:
import scalaz.Scalaz._
println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")
println("34.4".parseDouble) // Success(34.4)
No, that still doesn't work:"9d".parseDouble
->Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428800%2fscala-restricted-way-of-converting-string-to-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So, apparently, Java's java.lang.Double#parseDouble
is able to parse all kinds of strings, e.g. NaN
, Infinity
and other.
The answer to your problem seems to be BigDecimal(myString).toDouble
Out of curiosity, here is, what three different approaches return for various kinds of input strings:
def main(args: Array[String]): Unit = {
val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
val parsers = Seq(
("Double", (s: String) => s.toDouble),
("BigDecimal", (s: String) => BigDecimal(s).toDouble),
("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
)
for (string <- strings) {
println(s"n------------- $string ------------")
for ((name, parser) <- parsers) {
val result = Try(parser(string)) match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => ex.toString
}
println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
}
}
}
Result:
------------- Infinity ------------
Double -> Infinity
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"
------------- NaN ------------
Double -> NaN
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "NaN"
------------- 9.0d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9f ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9.0 ------------
Double -> 9.0
BigDecimal -> 9.0
NumberFormat -> 9.0
------------- 9.1 ------------
Double -> 9.1
BigDecimal -> 9.1
NumberFormat -> 9.1
------------- 1.4e14 ------------
Double -> 1.4E14
BigDecimal -> 1.4E14
NumberFormat -> 1.4
1
The second one doesn't work as desired:val n = NumberFormat.getNumberInstance.parse("9d")
results inn: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
add a comment |
So, apparently, Java's java.lang.Double#parseDouble
is able to parse all kinds of strings, e.g. NaN
, Infinity
and other.
The answer to your problem seems to be BigDecimal(myString).toDouble
Out of curiosity, here is, what three different approaches return for various kinds of input strings:
def main(args: Array[String]): Unit = {
val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
val parsers = Seq(
("Double", (s: String) => s.toDouble),
("BigDecimal", (s: String) => BigDecimal(s).toDouble),
("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
)
for (string <- strings) {
println(s"n------------- $string ------------")
for ((name, parser) <- parsers) {
val result = Try(parser(string)) match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => ex.toString
}
println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
}
}
}
Result:
------------- Infinity ------------
Double -> Infinity
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"
------------- NaN ------------
Double -> NaN
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "NaN"
------------- 9.0d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9f ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9.0 ------------
Double -> 9.0
BigDecimal -> 9.0
NumberFormat -> 9.0
------------- 9.1 ------------
Double -> 9.1
BigDecimal -> 9.1
NumberFormat -> 9.1
------------- 1.4e14 ------------
Double -> 1.4E14
BigDecimal -> 1.4E14
NumberFormat -> 1.4
1
The second one doesn't work as desired:val n = NumberFormat.getNumberInstance.parse("9d")
results inn: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
add a comment |
So, apparently, Java's java.lang.Double#parseDouble
is able to parse all kinds of strings, e.g. NaN
, Infinity
and other.
The answer to your problem seems to be BigDecimal(myString).toDouble
Out of curiosity, here is, what three different approaches return for various kinds of input strings:
def main(args: Array[String]): Unit = {
val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
val parsers = Seq(
("Double", (s: String) => s.toDouble),
("BigDecimal", (s: String) => BigDecimal(s).toDouble),
("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
)
for (string <- strings) {
println(s"n------------- $string ------------")
for ((name, parser) <- parsers) {
val result = Try(parser(string)) match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => ex.toString
}
println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
}
}
}
Result:
------------- Infinity ------------
Double -> Infinity
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"
------------- NaN ------------
Double -> NaN
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "NaN"
------------- 9.0d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9f ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9.0 ------------
Double -> 9.0
BigDecimal -> 9.0
NumberFormat -> 9.0
------------- 9.1 ------------
Double -> 9.1
BigDecimal -> 9.1
NumberFormat -> 9.1
------------- 1.4e14 ------------
Double -> 1.4E14
BigDecimal -> 1.4E14
NumberFormat -> 1.4
So, apparently, Java's java.lang.Double#parseDouble
is able to parse all kinds of strings, e.g. NaN
, Infinity
and other.
The answer to your problem seems to be BigDecimal(myString).toDouble
Out of curiosity, here is, what three different approaches return for various kinds of input strings:
def main(args: Array[String]): Unit = {
val strings = Seq("Infinity", "NaN", "9.0d", "9d", "9f", "9.0", "9.1", "1.4e14")
val parsers = Seq(
("Double", (s: String) => s.toDouble),
("BigDecimal", (s: String) => BigDecimal(s).toDouble),
("NumberFormat", (s: String) => NumberFormat.getNumberInstance.parse(s).doubleValue()),
)
for (string <- strings) {
println(s"n------------- $string ------------")
for ((name, parser) <- parsers) {
val result = Try(parser(string)) match {
case scala.util.Success(value) => value
case scala.util.Failure(ex) => ex.toString
}
println(name.reverse.padTo(20, " ").reverse.mkString + " -> " + result)
}
}
}
Result:
------------- Infinity ------------
Double -> Infinity
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "Infinity"
------------- NaN ------------
Double -> NaN
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> java.text.ParseException: Unparseable number: "NaN"
------------- 9.0d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9d ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9f ------------
Double -> 9.0
BigDecimal -> java.lang.NumberFormatException
NumberFormat -> 9.0
------------- 9.0 ------------
Double -> 9.0
BigDecimal -> 9.0
NumberFormat -> 9.0
------------- 9.1 ------------
Double -> 9.1
BigDecimal -> 9.1
NumberFormat -> 9.1
------------- 1.4e14 ------------
Double -> 1.4E14
BigDecimal -> 1.4E14
NumberFormat -> 1.4
edited Nov 22 '18 at 11:36
answered Nov 22 '18 at 11:12
ygorygor
1,1521616
1,1521616
1
The second one doesn't work as desired:val n = NumberFormat.getNumberInstance.parse("9d")
results inn: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
add a comment |
1
The second one doesn't work as desired:val n = NumberFormat.getNumberInstance.parse("9d")
results inn: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
1
1
The second one doesn't work as desired:
val n = NumberFormat.getNumberInstance.parse("9d")
results in n: Number = 9
– Kolmar
Nov 22 '18 at 11:29
The second one doesn't work as desired:
val n = NumberFormat.getNumberInstance.parse("9d")
results in n: Number = 9
– Kolmar
Nov 22 '18 at 11:29
For the second one to work, you'd need something like:
val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
For the second one to work, you'd need something like:
val s = "9d"; val pos = new ParsePosition(0); val n = NumberFormat.getNumberInstance.parse(s, pos); if (pos.getIndex != s.length) throw new NumberFormatException
– Kolmar
Nov 22 '18 at 11:33
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
Thanks, I edited my answer
– ygor
Nov 22 '18 at 11:38
add a comment |
I found a solution with scalaz
:
import scalaz.Scalaz._
println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")
println("34.4".parseDouble) // Success(34.4)
No, that still doesn't work:"9d".parseDouble
->Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
add a comment |
I found a solution with scalaz
:
import scalaz.Scalaz._
println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")
println("34.4".parseDouble) // Success(34.4)
No, that still doesn't work:"9d".parseDouble
->Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
add a comment |
I found a solution with scalaz
:
import scalaz.Scalaz._
println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")
println("34.4".parseDouble) // Success(34.4)
I found a solution with scalaz
:
import scalaz.Scalaz._
println("34b".parseDouble) // Failure(java.lang.NumberFormatException: For input string: "34b")
println("34.4".parseDouble) // Success(34.4)
answered Nov 22 '18 at 11:08
pmepme
3,61811933
3,61811933
No, that still doesn't work:"9d".parseDouble
->Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
add a comment |
No, that still doesn't work:"9d".parseDouble
->Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
No, that still doesn't work:
"9d".parseDouble
-> Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
No, that still doesn't work:
"9d".parseDouble
-> Success(9.0)
– Kolmar
Nov 22 '18 at 11:26
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
Strange in ScalaFiddle that works?!
– pme
Nov 22 '18 at 12:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428800%2fscala-restricted-way-of-converting-string-to-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Interesting question! This applies to pure Java too. One solution:
BigDecimal(myString).toDouble
– ygor
Nov 22 '18 at 11:01