Conversion of String having decimal values to Long

Posted By : Anil Kumar | 15-Nov-2017
Problem definition: Converts string to long may raise runtime exception (NumberFormatException) if value of string having decimal value.
 
 
Example:
 
 
 package test;

import java.math.BigDecimal;

public class stringtolong {

	public static void main(String[] args) {
		simple();
		tough();

	}
	
	static void simple(){
		String s = "10";
		// convert string to long
		long l = Long.valueOf(s);
		System.out.println("Value is"+ l);
	}
	
	static void tough(){
		String s = "10.95";
		// convert string to long
		long l = Long.valueOf(s);
		System.out.println("Value is"+ l);
	}

}
Output:
 
Value is10
Exception in thread "main" java.lang.NumberFormatException: For input string: "10.95"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.valueOf(Long.java:803)
at test.stringtolong.tough(stringtolong.java:24)
at test.stringtolong.main(stringtolong.java:10)
 
 
In the above example when string having the decimal value the runtime exception was thrown.
we have to hanlde this case there are 3 ways to handle such things 
1. Use try catch block 
2. Check the string variable is it has the decimal value or not. If it has the decimal value then split it from ".", 
And use the index one value (if it is not blancked)
3. Use the Bigdecimal value
 
By using only the first way we will save our functionality from breakdown. 
But it is unable to give the output of string to long value in case of string having decimal values.
 
 
Example:
 
package test;

import java.math.BigDecimal;

public class stringtolong {

	public static void main(String[] args) {
		simple();
		tough();

	}
	
	static void simple(){
		try{
			String s = "10";
			// convert string to long
			long l = Long.valueOf(s);
			System.out.println("Value is"+ l);
		}
		catch (NumberFormatException e) {
			System.out.println("String may have decimal values");
		}
	}
	
	static void tough(){
		try{
			String s = "10.95";
			// convert string to long
			long l = Long.valueOf(s);
			System.out.println("Value is"+ l);
		}
		catch (NumberFormatException e) {
			System.out.println("String may have decimal values");
		}
	}
	

}
 
Output:
 
Value is10
String may have decimal values
 
 
Its handle the exception but not gives the value.
For getting the value from string having decimal values we have to use either second or third way also.
In the below example we are useing the third way
 
 
Example:
 
package test;

import java.math.BigDecimal;

public class stringtolong {

	public static void main(String[] args) {

		simple();
		tough();
		solution();

	}
	
	static void simple(){
		try{
			String s = "10";
			// convert string to long
			long l = Long.valueOf(s);
			System.out.println("Value is"+ l);
		}
		catch (NumberFormatException e) {
			System.out.println("String may have decimal values");
		}
	}
	
	static void tough(){
		try{
			String s = "10.95";
			// convert string to long
			long l = Long.valueOf(s);
			System.out.println("Value is"+ l);
		}
		catch (NumberFormatException e) {
			System.out.println("String may have decimal values");
		}
	}
	
	static void solution(){
		try{
		String s = "10.95";
		// convert string to BigDecimal
		BigDecimal b = new BigDecimal(s);
		long l = b.longValue();
		System.out.println("Value is"+ l);
		}
		catch (NumberFormatException e) {
			System.out.println("Only numeric values are allowed");
		}
	}

}
Output:
 
Value is10
String may have decimal values
Value is10
Request for Proposal

Recaptcha is required.

Sending message..