Wednesday 27 January 2016

Java - Convert String containing decimal values to BigDecimal


Java Program to convert String containing decimal values into BigDecimal :
Sometime, we try to convert the String containing the decimal values into the Long. But since, int and long are Integer type so they can't have the decimal values to Long.valueOf(StringValue) gives the error.
The correct approach is to convert the string decimal into the BigDecimal or Double and use them as decimal values.

Example :
        String double_as_string = "23.23";
        BigDecimal price = new BigDecimal(double_as_string);

Tuesday 26 January 2016

Java - Removing the trailing zeroes from a long number

Java Program to remove the trailing zeroes from the long digit :
Trailing zeroes can be removed from the long digit by using the DecimalFormat class. Below is one such example.

import java.text.DecimalFormat;
public class removeTrailingZero {
public static void main(String[] args) {
double answer = 5.0;
double answer1 = 4.50;
double answer2 = 5.25;
  DecimalFormat df = new DecimalFormat("###.##");
 System.out.println(df.format(answer));  
 System.out.println(df.format(answer1));
 System.out.println(df.format(answer2));
String x = df.format(answer);
System.out.println(x);
}
}

Monday 11 January 2016

SQL - Adding a column in table from another table based on common column data in both table

We need to update values in table1 with data from table2 where common_column is the present in both tables and data should be updated based on this :

update table1 tl set table1_column=(select v.table2_column from table2 v where v.common_column= tl.common_column) where tl.table2_column is not null;  -- here condition can be any other condition 

Friday 8 January 2016

Program to remove white spaces and special character from String in JAVA

JAVA Program to remove white spaces and special characters from String:

public class testRegular {
public static void main(String[] args) {
String text = "This - word ! has \\ /allot # of % special % characters ? this12340   _ ASGHJKK \t";
text = text.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(text);
}}


Here it will remove everything except alphabets [ small and capital ] and digits [ 0 - 9].

Similarly, we can make our permutation and combination of  arrangements in replaceAll method.