I tried to get the local decimal separator with the following code:
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
//option 1
DecimalFormat format=(DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
public char sep=symbols.getDecimalSeparator();
//option 2
double value = 1234.5;
NumberFormat fmt = NumberFormat.getInstance();
String formatted = fmt.format(value);
public String decSep= formatted.substring(5,6);
//option 3
public Locale loc=Locale.getDefault();
DecimalFormatSymbols symbols= new DecimalFormatSymbols(loc);
public char sep=symbols.getDecimalSeparator();
public String decSep=Character.toString(sep);
but I always get "," even if the setting is set to "."
I tried with VB.NET code and it works.
What's wrong or missing in Java code ?
I appreciate your help
Try this :
Locale.setDefault(Locale.US);
This will set every output number with decimal separator. You can also take some input numbers with decimal separator.
Related
I have an issue where the format returned from the exact same methods are different in different java versions.
Currency currency = Currency.getInstance("CAD");
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setCurrency(currency);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(" " + formatter.format(10.00)) ;
It returns CA$10.00 in Java 11 but in Java 8 it's CAD10.00.
Is there any way I can get the prefix to always be CAD across Java versions? I know that currency.getCurrencyCode() returns CAD consistently across the versions. But I can't seem to find an overloaded method that let's me configure this.
The following works for me. Looking through the source code, it appears that the CA$ is the currency symbol. Seems like it was changed from CAD (in Java 8) to CA$ (in later versions). The following line of code actually returns a DecimalFormat instance.
NumberFormat formatter = NumberFormat.getCurrencyInstance();
You can explicitly set the currency symbol to CAD.
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Currency;
public class SchTasks {
public static void main(String args[]) {
Currency currency = Currency.getInstance("CAD");
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setCurrency(currency);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("CAD");
((DecimalFormat) formatter).setDecimalFormatSymbols(dfs);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(" " + formatter.format(10.00)) ;
}
}
You can run the above code from this JDoodle
https://www.jdoodle.com/iembed/v0/s67
This is the question and I am stuck that how can I append zeroes in place of empty spaces after the decimal in java.
enter link description here
Any string formatter can do it.
String s = String.format("%.30f", 1.23);
or
System.out.printf("%.30f %n", 1.23);
Those examples give you 30 places after the decimal point.
A java.text.DecimalFormat instance can do this for you, here is an example:
new DecimalFormat("#,##0.00000").format(1.23); // => 1.23000
new DecimalFormat("#,##0.00000").format(.987643); // => 0.98764
You can use DecimalFormat#format to do so.
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
// Define the formatter
NumberFormat formatter = new DecimalFormat("0.000000");
// Tests
System.out.println(formatter.format(0.3));
System.out.println(formatter.format(123.3));
System.out.println(formatter.format(0.335));
System.out.println(formatter.format(0.0));
System.out.println(formatter.format(1.0));
}
}
Output:
0.300000
123.300000
0.335000
0.000000
1.000000
I have double var and I need to make it in 0.00 format. Works fine with this:
sum = Math.round(sum*100.00)/100.00;
I return it by
return Double.toString(sum);
But instead of for example 2.40 it gives me 2.4 (missing 0 at the end).
I have these imports available:
import static org.junit.Assert.*; import java.util.*;import org.junit.Test;
I solved the problem with DecimalFormat and BigDecimal, but i can't use those libraries.
You can simple format the String
double num = 2.402;
String output = String.format("%.2f", num);
System.out.println(output);
You can use java.lang.String#format() or java.util.Formatter to print the number in a convenient manner.
return String.format("%.2f", sum);
should do the job.
Simply use format
example:
package com.test;
import java.lang.*;
import java.util.*;
public class StringDemo {
public static void main(String[] args) {
double piVal = Math.PI;
/* returns a formatted string using the specified format
string, and arguments */
System.out.format("%f\n", piVal);
}
}
output: 3.141593
The %f allows for the amount of digits you wanna incudes ot %.2f includes 2 digits after.
I'm trying to make a double to be this format 00,00
Example
9,21341 > 09,21
10,4312 > 10,43
1,01233 > 01,01
42,543 > 42,54
Currently I'm using the String.Format to round the double
String.format("%s %02.2f - ", example.getName(), example.getDouble());
This does not add the extra zero in front of the double if it is smaller than 10.
Formatter class (which is the basis of String.format method) operates using the concept of "fields". That is, each field is of some specific size and can be padded. In your case, you could use a formating like %05.2f, which would mean a field of size 5, with 2 symbols after the point padded with zeroes to the left.
However, if you need some fine-grained formatting of numbers usually what you are looking for is DecimalFormat class, which allows you to easily customize how the numbers are represented.
Example (ideone link):
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
DecimalFormat decimalFormat = new DecimalFormat("#00.00");
System.out.println(decimalFormat.format(0.99f));
System.out.println(decimalFormat.format(9.99f));
System.out.println(decimalFormat.format(19.99f));
System.out.println(decimalFormat.format(119.99f));
}
}
Output:
00.99
09.99
19.99
119.99
I am trying to format a Number with DecimalFormat. But I want it to format a number, that is like
input: 1234. --> should be formatted to: 1,234.
But I get 1,234.0 or 1,234.00 depending on my rules for the decimal format
What do I have to do in order to get this done?
The methods that should help you are setMinimumFractionDigits and setMaximumFractionDigits.
format.setMinimumFractionDigits(0);
at a guess, is probably what your looking for.
To ensure that the decimal separator is always shown, use: DecimalFormat.setDecimalSeparatorAlwaysShown(true)
You could format the number regardless of whether it is a decimal or not by using
DecimalFormat f = new DecimalFormat("#,###");
f.format(whatever)...
If you don't want to display any decimal places, don't format a floating point value :) If you use BigInteger, int, or long, it should be fine:
import java.math.*;
import java.text.*;
public class Test {
private static final char p = 'p';
public static void main(String[] args) {
NumberFormat format = new DecimalFormat();
BigInteger value = BigInteger.valueOf(1234);
System.out.println(format.format(value));
System.out.println(format.format(1234));
System.out.println(format.format(1234L));
}
}
Try this:
DecimalFormat df = new DecimalFormat("#,###.", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
System.out.println(df.format(1234));