Java floating point number in comma instead of dot
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));
How to set the floating point symbol in JSpinner to dot instead of comma if the locale is french?
I found the solution. When you use the method Locale.setDefault(Locale)
it sets the default locale for display and also for formatting, but in my case, I need only to display my user interfaces in French. For that, I have to use also the second overload of the method : setDefault(Locale.Category, Locale)
, where the first parameter indicate if I want to use the locale for displaying user interfaces or for formatting date, numbers or curencies. The main method now is like this :
Locale.setDefault(Locale.FRENCH); // Display the user interfaces in French
Locale.setDefault(Locale.Category.FORMAT, Locale.ENGLISH); // But the formatting in English
NumberFormatException caused by a float having a comma instead of dot as decimal seperator
That seems likely to be related to running in a different Locale (such as Germany) where the decimal separator character is ','.
If you are actually parsing user input, you need to use a NumberFormat to parse the data.
Making doubles use commas instead of dots without importing formatting
Because you have said in your desired answer "123456789.0 needs to shown as 123,456,789" I am assuming you don't care about the single decimal place.
Wrap it with Decimal formatter:
DecimalFormat formatter = new DecimalFormat("###,###");
// Now wrap each and every desiredmethod output like formatter.format(methodOutput())
System.out.println("Name of dam:\t\t" + getName() +
"\nYear opened:\t\t" + getYear() +
"\nAge [yrs}:\t\t" + getAge() +
"\nDate as of:\t\t" + getDate() +
"\nStorage:\t\t" + getStorage() +
"\nCapacity:\t\t" + getCapacity() +
"\nInflow:\t\t\t" + formatter.format(getInflow()) +
"\nOutflow:\t\t" + formatter.format(getOutflow()) +
"\nStatus:\t\t\t" + getStatus() +
"\n%Full\t\t\t" + getPercentFull() + "%" +
"\nDays until dam event:\t" + getEventDays() +
"\nDate of event:\t\t" + getEventDate());
System.out.println();
I didn't know which other ones you wanted, so only did for inFlow and outFlow. For reference - you can check https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
If you don't want to use Formatters, you can use plain String.format("%,.0f", yourDoubleValue)
Formatting doubles to two decimal places in Java produces a comma instead of a dot
If you want a dot rather than a comma, you should specify a Locale
which uses dot as the decimal separator, e.g.
DecimalFormat df = new DecimalFormat("#.00",
DecimalFormatSymbols.getInstance(Locale.US));
Basically, "." in a format pattern doesn't mean "dot", it means "decimal separator".
format float[] to String decimal sign changes to comma
That is because String.format()
is locale-aware. I'm guessing your default locale is Locale.GERMAN
, since Locale.NORWEGIAN
does not exist. And the decimal separator is a comma.
The locale always used is the one returned by
Locale.getDefault()
.
If you want to format according to a specific locale, then you should use
Locale myLocale = ...;
String.format(myLocale, "%.2f", myNum[0]);
The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified. It can be changed using the setDefault
method. You can easily check your locale: System.out.println(Locale.getDefault())
.
Related Topics
Junit 5 - No Parameterresolver Registered for Parameter
Check If One List Contains Element from the Other
Java.Exe Finished With Non-Zero Exit Value 1
Convert String With Dot or Comma to Float Number
Regex to Match Substring After Nth Occurence of Pipe Character
Tomcat Server Error - Port 8080 Already in Use
Java 8 Convert Utc Time to Edt/Est So That Date Remains the Same
Checking If a Document Exists in a Firestore Collection
Regex to Match a String Not Starting or Ending With a Pattern
How to Write a User Input Based Constructor
Java:Using Apache Poi How to Convert Ms Word File to Pdf
Duplicate Entry Exception: Spring Hibernate/Jpa Cascade Save Many to One
How to Get Numbers from Given Gcd and Lcm
Spring Boot JPA Unknown Column in Field List
Pass Multiple Parameters to Rest API - Spring
Dynamically Change Application.Properties Values in Spring Boot