Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, July 28, 2009

Case Insensitive Replace in Java

public class TestReplaceAll {
public static void main(String[] args) {
String str = "I am a programMeR";
System.out.println("input : " + str);
str = str.replaceAll("(?i)programmer", "developer");
System.out.println("output : " + str);
}
}

This program might be helpful in replacing words which are not consistent with respect to case in a string. This piece of code was given by a colleague.

Sunday, July 26, 2009

Java - Rounding floating point numbers to a certain number of decimal places

Small piece of code to round decimal numbers to specified number of decimal places.
public float Round(float val, int roundOffTo){
float p = (float)Math.pow(10,roundOffTo);
val = val * p;
float tmp = Math.round(val);
return (float)tmp/p;
}