All I have a floating point number in Finnish local. It is like the following:-
String numberString = "1000,30";
if(numberString.contains(",")){
numberString = numberString.replaceAll(",",".").trim();
}
try {
Number number = Double.parseDouble(numberString);
return number;
}catch (NumberFormatException ex){
System.out.printf(ex.getMessage());
}
return null;
But this number has value 1000.30. I would like the number should I have value 1000,30. How can I have Number with the comma instead of the dot?
This is a basic question it must have been asked earlier. But all I find is in String data type. I don't see any Number data type.
When seeing your comment that the API accepts only Number and that it calls Number#toString() on it, then I see only 1 way to enforce the rightful display. By using your own implementation of Number and overwriting the way Object#toString() works:
public class CorrectlyDisplayedDouble extends Number{
private final Double internal;
public CorrectlyDisplayedDouble(Double internal){
this.internal = internal;
}
public int intValue(){
return internal.intValue();
}
public long longValue(){
return internal.longValue();
}
public float floatValue(){
return internal.floatValue();
}
public double doubleValue(){
return internal.doubleValue();
}
public String toString(){
// replaces periods with commas
return internal.toString().replace(".", ",");
}
}
Which can then be easily created using following snippet, which then also can be passed to your third party API:
Number number = new CorrectlyDisplayedDouble(Double.parseDouble(numberString));
Sample Code :
String number = "1000500000.574";
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(amount));
This will help you to understand how Locale exactly works:
public double parse(String decimalAsText) {
NumberFormat decimalFormat = NumberFormat.getNumberInstance(Locale.FRANCE);
decimalAsText = decimalAsText.replace(' ', '\u00a0');
ParsePosition pp = new ParsePosition(0);
Number n = decimalFormat.parse(decimalAsText, pp);
return n.doubleValue();
}
public String parse(double textAsDecimal) {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
BigDecimal bd = BigDecimal.valueOf(textAsDecimal);
nf.setMaximumFractionDigits(bd.scale());
String s = nf.format(textAsDecimal);
return s;
}
You can use DecimalFormat and Locale, for example:
DecimalFormat dfFrance = (DecimalFormat)DecimalFormat.getInstance(Locale.FRANCE);
dfFrance.applyPattern("###.00");
Number n = 0;
try {
n = dfFrance.parse("1000,30");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(n.doubleValue());
Related
This question already has an answer here:
Print float with two decimals unless number is a mathematical integer
(1 answer)
Closed 2 years ago.
I need format numbers like this:
23.0 -> 23
23.20 -> 23.20
23.00 -> 23
23.11 -> 23.11
23.2 -> 23.20
23.999 -> 24
23.001 -> 23
1345.999 -> 1346 // Edited the question to add this from the OP's comment
Here is my code:
java:
public static String toPrice(double number) {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
DecimalFormat format = new DecimalFormat("#,###,###.##", formatSymbols);
return format.format(number);
}
kotlin:
fun Double.toPrice(): String = DecimalFormat("#,###,###.##", DecimalFormatSymbols().apply {
groupingSeparator = ' '
}).format(this)
But for input 23.20 or 23.2 I get the result 23.2. This is wrong for me. I need 23.20. Which string pattern should I use to achieve this result? Please, help me.
public static String toPrice(double number) {
if (number == (int) number) {
return Integer.toString((int) number);
} else {
return String.format("%.2f", number);
}
}
EDITTo include thousand separator
public static String toPrice(double number) {
if (number == (int) number) {
return String.format("%,d",(int)number);
} else {
return String.format("%,.2f", number);
}
}
Update
In case you need to format 1345.999 as 1346 (as mentioned in your comment), you can do so by adding a couple of extra steps in the original answer:
Format the number with the pattern, #,###,###.00 and then remove all commas so that it can be parsed back to double.
Parse the converted string into double and follow the rest of the solution as mentioned in the original answer.
Demo:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
System.out.println(getFromattedNumber(1234567.0));
System.out.println(getFromattedNumber(1234567.20));
System.out.println(getFromattedNumber(1234567.01));
System.out.println(getFromattedNumber(1234567.00));
System.out.println(getFromattedNumber(1345.999));
}
static String getFromattedNumber(double number) {
NumberFormat format = new DecimalFormat("#,###,###.00");
double num = Double.parseDouble(format.format(number).replace(",", ""));
// Format for integer decimal numbers
NumberFormat formatInt = new DecimalFormat("#,###,###");
if ((int) num == num) {
return formatInt.format(number);
} else {
return format.format(number);
}
}
}
Output:
1,234,567
1,234,567.20
1,234,567.01
1,234,567
1,346
Original answer
You will need to use two formats as shown below:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
System.out.println(getFromattedNumber(1234567.0));
System.out.println(getFromattedNumber(1234567.20));
System.out.println(getFromattedNumber(1234567.01));
System.out.println(getFromattedNumber(1234567.00));
}
static String getFromattedNumber(double number) {
// Format for integer decimal numbers
NumberFormat format1 = new DecimalFormat("#,###,###");
// Format for non-integer decimal numbers
NumberFormat format2 = new DecimalFormat("#,###,###.00");
if ((int) number == number) {
return format1.format(number);
} else {
return format2.format(number);
}
}
}
Output:
1,234,567
1,234,567.20
1,234,567.01
1,234,567
I do a number formatting to indicate currency values as follows :
private static DecimalFormat decimalFormat = new DecimalFormat("0.00");
List<String> paidAmounts = new ArrayList<>();
for (Invoice invoice : invoices) {
paidAmounts.add(String.valueOf
("$"+Formatter.currencyDecimalFormat(invoice.getAmount())));
}
public static Float currencyDecimalFormat(int value) {
return Float.valueOf(decimalFormat.format((float) value / 100));
}
Invoice class :
private int amount;
public int getAmount() {
return amount;
}
But the values which ends with '0' are not formatted to the desirable format.
$18.5
$16.8
$105.59
How to make this correct?
Your Float.valueOf is reformatting the float to remove the trailing 0 because a float cannot hold a trailing 0. The String that .format returns can. If you try printing out:
decimalFormat.format((float) value / 100)
The float numbers will print correctly. Remove this call to .valueOf and return a String from currencyDecimalFormatinstead:
public static String currencyDecimalFormat(int value) {
return decimalFormat.format((float) value / 100);
}
I have a requirement were depending on a particular key value of a map , i need to format the output .
For example if its value greater than 1 then needed to display only 2 decimal points after the value
(12.23) or else if its value is less than 1 , i need to show 4 decimal points after it .
I have written the code its working fine , but i am looking for a better way of doing this (basically i didn't liked if else conditions in my code )
This is my program where depending on the last attribute key value i am formatting the output
package com;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class CustValues {
public static void main(String args[]) {
Map valuesMap = new HashMap();
valuesMap.put("mktCap", "12.4d");
valuesMap.put("last", "0.4344");
valuesMap.put("avgvalue", "34.55");
valuesMap.put("bidprice", "44.44");
Iterator<String> iterator = valuesMap.keySet().iterator();
while (iterator.hasNext()) {
String name = iterator.next().toString();
String value = (String) valuesMap.get(name);
if (name.equals("last")) {
String result = "";
double d = Double.parseDouble(value);
if (d > 1) {
result = formatNumber(value, 2);
} else {
result = formatNumber(value, 4);
}
System.out.println(result);
}
}
}
public static String formatNumber(String s, int decPts) {
double d = Double.parseDouble(s);
if (2 == decPts)
return new DecimalFormat("#,###,###,##0.00").format(d);
else if (0 == decPts)
return new DecimalFormat("#,###,###,##0").format(d);
else if (3 == decPts)
return new DecimalFormat("#,###,###,##0.000").format(d);
else if (4 == decPts)
return new DecimalFormat("0.0000").format(d);
return String.valueOf(d);
}
}
You could create a Map<Integer, DecimalFormat> formats (or a List<DecimalFormat>, if you prefer). Then formatNumber() simply calls formats.get(decPts) to get the correct format.
The logic you're implementing in the formatNumber method is the perfect candidate for a switch statement. Try
switch (decPts) {
case 0:
return new DecimalFormat("#,###,###,##0").format(d);
break;
case 2:
return new DecimalFormat("#,###,###,##0.00").format(d);
break;
...
}
For more info see this tutorial.
edit: Although SJuan76 beat me to it, and I like Code-Guru's idea better!
Building on Code-Guru's answer. You can use a map but to retain the same thread safety and default behavior the code becomes:
public class CustValues {
private static final Map<Integer, String> FORMATS;
static {
Map<Integer, String> formats = new HashMap<Integer, String>();
formats.put( 0, "#,###,###,##0" );
formats.put( 2, "#,###,###,##0.00" );
formats.put( 3, "#,###,###,##0.000" );
formats.put( 4, "0.0000" );
FORMATS = Collections.unmodifiableMap( formats );
}
public static void main(String args[]) {
// Same as before....
}
public static String formatNumber(String s, int decPts) {
double d = Double.parseDouble(s);
String format = FORMATS.get(decPts);
if( format != null ) {
return new DecimalFormat(format).format(d);
}
return String.valueOf(d);
}
}
You need to create a new DecimalFormat for each request instead of reusing it since it is not thread safe. This also handles the cases where decPts is not 0, 2, 3, or 4.
I have method as below:
public static String formatNumber(String s, int decPts) {
double d = Double.parseDouble(s);
if (decPts >= 0 && decPts <= 4) {
DecimalFormat df = new DecimalFormat("###,##0");
df.setMaximumFractionDigits(decPts);
df.setMinimumFractionDigits(decPts);
return df.format(d);
}
return String.valueOf(d);
}
You can use the switch sentence
switch (decPts) {
case 0:
return new DecimalFormat("#,###,###,##0").format(d);
case 2:
...
}
It helps tidy the code for this case. Anyway you would not be able to program in any language without using ìf or similar constructs.
Enums have the advantage of avoiding boxing and unboxing an integer that a map.get call would perform, while only creating the formatters you need one time. Note, you can also could get rid of the second double parse:
enum DisplayFormat {
CURRENCY(new DecimalFormat("#,###,###,#00.00")),
SMALL_CURRENCY(new DecimalFormat("0.000"));
private DecimalFormat f;
public DisplayFormat(DecimalFormat f) {
this.f = f;
}
public String format(double d) {
return this.f.format(d);
}
// Usage:
if (name.equals("last")) {
String result = "";
double d = Double.parseDouble(value);
if (d > 1) {
result = DisplayFormat.SMALL_CURRENCY.format(d)
} else {
result = DisplayFormat.CURRENCY.format(d)
}
System.out.println(result);
}
One really inefficient, but more flexible option would be to generate the format string based on decPts:
public static String formatNumber(String s, int decPts) {
double d = Double.parseDouble(s);
final String baseFormat = "#,###,###,##0";
// TODO: Use a StringBuilder
String format = decPts==0 ? baseFormat : baseFormat + ".";
for (int i=0; i < decPts; i++) {
format += "0";
}
return new DecimalFormat(format).format(d);
}
Again, this is a bad idea unless you need to show an arbitrary number of decimal points or can only determine the way to display the number at run-time, which probably isn't the case.
I need percentage form of a double value, so I used NumberFormat
double d = 0.13;
NumberFormat nf = NumberFormat.getPercentInstance();
String kpr = nf.format(kpr);
I saved this property to an object Person through setter. When I get it back through getter method it returns String(of course).
Is there a way to de-format this String to double value as 0.13, so that I can perform arithmetics on it?
public static void main(String s[]) {
getDoubleFromStringWithPercent("12345%");
}
private static Double getDoubleFromStringWithPercent(String string) {
Locale locale = Locale.CANADA;
Double num = null;
try {
Number number = NumberFormat.getPercentInstance(locale).parse(string);
if (number instanceof Long) {
num = ((Long) number).doubleValue();
} else {
num = (Double) number;
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
return num;
}
Locale need for find decimal delimiter ("." or ",");
Alternative version:
String string ="2165%";
double d = Double.parseDouble(string.substring(0, string.length()-1));
if (d != 0) {
d = d / 100;
}
double d=(Double.parase(kpr.substring(kpr.length-1)))/100;
I come across few of the times called helper objects... can anybody elaborate what are those helper objects and why do we need them?
Some operations which are common to a couple of classes can be moved to helper classes, which are then used via object composition:
public class OrderService {
private PriceHelper priceHelper = new PriceHelper();
public double calculateOrderPrice(order) {
double price = 0;
for (Item item : order.getItems()) {
double += priceHelper.calculatePrice(item.getProduct());
}
}
}
public class ProductService {
private PriceHelper priceHelper = new PriceHelper();
public double getProductPrice(Product product) {
return priceHelper.calculatePrice(product);
}
}
Using helper classes can be done in multiple ways:
Instantiating them directly (as above)
via dependency injection
by making their methods static and accessing them in a static way, like IOUtils.closeQuietly(inputStream) closes an InputStream wihtout throwing exceptions.
at least my convention is to name classes with only static methods and not dependencies XUtils, and classees that in turn have dependencies / need to be managed by a DI container XHelper
(The example above is just a sample - it shouldn't be discussed in terms of Domain Driven Design)
These are objects that "sit to the side" of the main body of code, and do some of the work for the object. They "help" the object to do it's job.
As an example, many people have a Closer helper object. This will take various closeable objects, for example, java.sql.Statement, java.sql.Connection, etc and will close the object, and ignore any errors that come out of it. This tends to be because if you get an error closing an object, there is not much you can do about it anyway, so people just ignore it.
Rather than having this boilerplate:
try {
connection.close();
} catch (SQLException e) {
// just ignore… what can you do when you can't close the connection?
log.warn("couldn't close connection", e);
}
scattered around the codebase, they simply call:
Closer.close(connection);
instead. For example, look at guava closeQuietly.
A 'helper' method is typically a method to make something easier, whatever it is. Sometimes they're used to make things more readable/clearly organized (some may argue this, but it's ultimately very subjective):
public void doStuff() {
wakeUp();
drinkCoffee();
drive();
work();
goHome();
}
Where, each 'helper method' on their own are fairly complex... the concept becomes really clear and simple.
Another very good use of helper methods is to provide common functionality across many different classes. The best example of this is the Math class which contains a ton of static helper methods to help you calculate things like the log of a number, the exponent of a number... etc.
Where you draw the line as to what's a helper method and what's just a regular method is pretty subjective, but that's the gist of it. Other answers here are pretty good too.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Helpers {
public static String getDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
return dateFormat.format(new Date());
}
public static boolean isTimeABeforeTimeB(String timeA, String timeB) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
Date dA = dateFormat.parse(timeA);
Date dB = dateFormat.parse(timeB);
if (dA.getTime() < dB.getTime()) {
return true;
} else {
return false;
}
} catch (Exception e) {
//
}
return false;
}
public static String getDateAndTimeInput(String prompt) {
Scanner input = new Scanner(System.in);
String ans;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
dateFormat.setLenient(false);
boolean dateValid;
do {
System.out.print(prompt);
ans = input.nextLine();
ans = ans.trim();
dateValid = true;
try {
Date d = dateFormat.parse(ans);
} catch (Exception e) {
dateValid = false;
}
} while (!dateValid);
return ans;
}
public static String getStringInput(String prompt) {
Scanner input = new Scanner(System.in);
String ans;
do {
System.out.print(prompt);
ans = input.nextLine();
ans = ans.trim();
} while (ans.length() == 0);
return ans;
}
public static double getDoubleInput(String prompt) {
Scanner input = new Scanner(System.in);
double ans = 0;
boolean inputValid;
do {
System.out.print(prompt);
String s = input.nextLine();
//Convert string input to integer
try {
ans = Double.parseDouble(s);
inputValid = true;
} catch (Exception e) {
inputValid = false;
}
} while (!inputValid);
return ans;
}
public static int getIntegerInput(String prompt) {
Scanner input = new Scanner(System.in);
int ans = 0;
boolean inputValid;
do {
System.out.print(prompt);
String s = input.nextLine();
// Convert string input to integer
try {
ans = Integer.parseInt(s);
inputValid = true;
} catch (Exception e) {
inputValid = false;
}
} while (!inputValid);
return ans;
}
public static int getIntegerInput(String prompt, int lowerBound, int upperBound) {
Scanner input = new Scanner(System.in);
int ans = 0;
boolean inputValid;
do {
System.out.print(prompt);
String s = input.nextLine();
// Convert string input to integer
try {
ans = Integer.parseInt(s);
if (ans >= lowerBound && ans <= upperBound) {
inputValid = true;
} else {
inputValid = false;
}
} catch (Exception e) {
inputValid = false;
}
} while (!inputValid);
return ans;
}
}
that is an example of of a Helper Class. It contains method which of are common use of the other classes in the project.
Example if someone wants to enter an Integer number from a class hew ill have to type in this: String num = Helpers.getIntegerInput("input your number");
The prompt is the output that is show to the user. Other examples to input a String, double, date and time etc.
Helper class, in my opinion, is similar to normal functions declared outside of classes in C++. For example, if you need a global constant for many classes, then you can define a helper class that encloses a final static const variable.
You can see a helper class as a toolbox that can be used by other classes to perform task like testing if a string is a palindrome, if a given number is prime, if an array contains negative number etc. You can create helper class by making all its methods static and its constructor private, and optionally you can also make the class final. Thus it can not be instantiated and one can easily access to all its methods directly.
public final class HelperClass{
private HelperClass(){
}
public static boolean isPositive(int number) {
if (number >= 0)
return true;
else
return false;
}
}
Here the function can be use directly to test a number :
HelperClass.isPositive(5);
this helper class help you to validate multiple edit text fields at once
public class MyHelperClass {
public void toast(Context context, String message){
Toast toast=new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setText(message);
toast.show();
}
public void validateEditText(Context context,List<EditText> editTextList)
{
boolean result=false;
for (int i=0;i<editTextList.size();i++)
{
if (editTextList.get(i).getText().toString().trim().equals(""))
{
result=true;
toast(context," Required fields are not empty !");
i=editTextList.size();
}
}
}
}