java.lang.Double parseDouble(String s)
Syntax:
public static double parseDouble(String s) throws NumberFormatException
Java Code Example :
This java example source code demonstrates the use of parseDouble(String s) method of Double class. This method returns the primitive double value of the string method input.
To make this example sensible we will calculate the tax incurred from the user input.
package com.javatutorialhq.java.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; /* * This example source code demonstrates the use of static method * parseDouble(String str) of Double class */ public class DoubleParseDoublefString { public static void main(String[] args) { /* * Example on how to calculate the tax incurred * given the amount and tax percentage from user input */ try { System.out.println("*******Code to " + "calculate the tax incurred******"); System.out.print("Enter Amount:"); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); double amount = Double.parseDouble(br.readLine()); System.out.print("Enter the tax percentage:"); br = new BufferedReader( new InputStreamReader(System.in)); double percent = Double.parseDouble(br.readLine()); double taxIncurred = percent/100 * amount; System.out.println("\n******************"); System.out.println("Tax = " + taxIncurred); br.close(); } catch (NumberFormatException | IOException e) { e.printStackTrace(); } } }
Sample Output :
Running the above example source code will give the following output