Thousend seperator function - (1,00,000)

Normally if we write a number that is more than 1000, we normally write it with thousend seperator commas other than write it at once.
Eg :
If we want to write 203465397465 we write it as
number : 203,465,397,465
this way we can read the number more easily than other way.
So after adding commas to that number the number no longer be a in number format.
We cant add, divide, substract.....etc.

public String convertThousendsWithCommas(String num) {
String pattern = "#,###.##;-#,###.##"; //pattern that we want
double value = 0.0;
String out = null;
try {
value = Double.parseDouble(num);
NumberFormat nf = NumberFormat.getInstance();
if (nf instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) nf;
df.applyPattern(pattern);
out = df.format(value);
}
} catch (NumberFormatException n){
System.out.print("not a number:");
out = num;
}
return out;
}

Comments

Popular posts from this blog

PostgreSQL bytea and oid

MySQL as Hive metadata store

Microservices Architecture with Spring Boot in 15mins