May times we need to convert a string into a Double value. This can be achieved by various functions in various programming languages.
Swift
1 2 3 4 |
let string = “15.666” let myDouble = Double(string) ?? 0 print(myDouble) |
Also can be done by following method.
1 2 3 4 5 6 |
import Foundation let string = “15.666” let myDouble = (string as NSString).doubleValue print(myDouble) |
Kotlin
1 2 3 4 5 6 7 8 9 |
fun myParseDouble(inputString: String): Double { val result: Double? try { result = java.lang.Double.parseDouble(inputString) return result } catch (e: Exception) { return 0.0 } } |
JAVA
1 2 3 4 5 6 7 8 9 |
function Double myParseDouble(String inputString){ Double result; try { result = java.lang.Double.parseDouble(inputString); return result; } catch (e: Exception) { return 0.0; } } |
PHP
In PHP doubleval() is just an alias for floatval().
1 2 3 |
$var = ‘122.34343’; $float_value_of_var = floatval($var); echo $float_value_of_var; // 122.34343 |
JAVAScript
1 2 3 4 |
var string = “10”; var double = parseFloat(string); console.log(typeof(double)); // float // parseFloat used for float and double datatypes |