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;
Related
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());
In Java, I am trying to parse a string of format "0.##" to a float. The string should always have 2 decimal places.
public ArrayList getFixRateDetails (String merchantccy,String paymodetype,String amount)
{
String returnAmt ="";
ArrayList list= new ArrayList();
float value=-1;
try {
NiCurrencyConverterProcessor nicc= new NiCurrencyConverterProcessor();
list=nicc.getFixRateDetails(merchantccy,paymodetype);
Float i = (Float.parseFloat((String) list.get(0)));
Float j = (Float.parseFloat(amount));
value=i*j;
list.set(0, value);
list.add(2, i);
GenericExceptionLog.log("PayPalUtility.java : In getFixRateDetails() : value:"+list,"paymenttarasectionsevlet111");
DecimalFormat df = new DecimalFormat("0.00");
String ReturnAmt = df.format(returnAmt);
returnAmt=String.valueOf(value).trim();
GenericExceptionLog.log("PayPalUtility.java : In getFixRateDetails() : value:"+returnAmt,"paymenttarasectionsevlet222");
} catch (Throwable t) {
GenericAsyncLogger.Logger(t,"ERROR","DB","Transaction","MIGSTxnUtility.java","postingAmtConversion()","NA","NA","","","paymenttarasectionsevlet");
//GenericExceptionLog.exceptionJava(t,"postingAmtConversion()", "MIGSTxnUtility");
}
return list;
}
}
You are assigning the formatted output to the different variable (ReturnAmt) and in the log you are printing returnAmt. It is obvious that it will print the value without formatting.
Try
DecimalFormat formatter = new DecimalFormat("0.00");
returnAmt = formatter.format(value);
For more information refer DecimalFormat
Example:
public static void main(String...args){
String returnAmt ="";
Float i = 100f;
Float j = 200f;
Float value=i*j;
DecimalFormat formatter = new DecimalFormat("0.00");
returnAmt = formatter.format(value);
System.out.println(returnAmt);
}
OUTPUT:
20000.00
I have tried to write TestFraction in such a way that the main method calls on the methods getFraction1 and getFraction2 to create two Fractions: fr1 (a/b) and fr2 (a/b). getFraction1 and getFraction2 prompt the user for two integers, a and b, and call on getNumber to capture these integers. Fraction then performs calculations on fr1 and fr2. The problem is, when I run TestFraction, the values of a and b are left at 1, which is what they're set to in Fraction. Can anyone give me a hint as to why getFraction1 and getFraction2 aren't actually passing the values of a and b into fr1 and fr2?
Here is my code for TestFraction:
package Fraction;
import java.io.*;
import java.util.*;
public class TestFraction
{
static Scanner console = new Scanner (System.in);
private static int getNumber() throws InputMismatchException
{
return console.nextInt();
}
private static Fraction getFraction1()
{
int a=1, b=1;
Fraction frac = new Fraction ();
while (true) {
// prompt to enter a numerator and a denominator
System.out.println("Input 1st fraction numerator and denominator");
// input the numerator and the denominator using getNumber()
try {
a = getNumber();
b = getNumber();
}
catch (Exception e)
{
System.out.println("Exception: "+e.toString());
console.nextLine();
continue;
}
return frac;
// return new Fraction if OK
// otherwise print an error message
}
}
private static Fraction getFraction2()
{
int a=1, b=1;
Fraction frac = new Fraction ();
while (true) {
// prompt to enter a numerator and a denominator
System.out.println("Input 2nd fraction numerator and denominator");
// input the numerator and the denominator using getNumber()
try {
a = getNumber();
b = getNumber();
}
catch (Exception e)
{
System.out.println("Exception: "+e.toString());
console.nextLine();
continue;
}
return frac;
// return new Fraction if OK
// otherwise print an error message
}
}
public static void main(String[] args)
{
Fraction fr1 = new Fraction ();
fr1 = getFraction1();
Fraction fr2 = new Fraction ();
fr2 = getFraction2();
Fraction res = new Fraction();
// define other variables including res and fr2
res = Fraction.add (fr1, fr2);
System.out.println(fr1+" + "+fr2+" = "+res);
res = Fraction.subtract (fr1, fr2);
System.out.println(fr1+" - "+fr2+" = "+res);
res = Fraction.multiply (fr1, fr2);
System.out.println(fr1+" * "+fr2+" = "+res);
res = Fraction.divide (fr1, fr2);
System.out.println(fr1+" / "+fr2+" = "+res);
res = Fraction.lessThan (fr1, fr2);
System.out.println(fr1+" "+res+" "+fr2);
// test subtract, multiply, divide, lessThan methods
// each test has to print a description, a result,
// and, possibly, a error message if the calculation fails
}
}
And here is Fraction:
package Fraction;
public class Fraction
{
protected int a;
protected int b;
public Fraction()
{
a = 1;
b = 1;
}
public Fraction (int a, int b)
{
this.a=a;
this.b=b;
}
public int getNumerator()
{
return a;
}
public int getDenominator()
{
return b;
}
public void setNumerator(int a)
{
this.a=a;
}
public void setDenominator(int b)
{
this.b=b;
}
public String toString ()
{
return a+"/"+b;
}
public int gcd(int a, int b)
{
//ToDo implement Euclide algorithm
if (b==0) return a;
return gcd(b, a%b);
}
public void lowestTerms()
{
int g=gcd(a,b);
a=a/g;
b=b/g;
}
public static Fraction add(Fraction first, Fraction second)
{
Fraction result = new Fraction();
result.setNumerator(first.getNumerator()*second.getDenominator()
+ first.getDenominator()*second.getNumerator());
result.setDenominator(first.getDenominator()*second.getDenominator());
result.lowestTerms();
return result;
}
//ToDo methods subtract, multiply, divide, lessThan
public static Fraction subtract(Fraction first, Fraction second)
{
Fraction result = new Fraction();
result.setNumerator(first.getNumerator()*second.getDenominator()
- first.getDenominator()*second.getNumerator());
result.setDenominator(first.getDenominator()*second.getDenominator());
result.lowestTerms();
return result;
}
public static Fraction multiply(Fraction first, Fraction second)
{
Fraction result = new Fraction();
result.setNumerator(first.getNumerator()*second.getNumerator());
result.setDenominator(first.getDenominator()*second.getDenominator());
result.lowestTerms();
return result;
}
public static Fraction divide(Fraction first, Fraction second)
{
Fraction result = new Fraction();
result.setNumerator(first.getNumerator()*second.getDenominator());
result.setDenominator(first.getDenominator()*second.getNumerator());
result.lowestTerms();
return result;
}
public static Fraction lessThan(Fraction first, Fraction second)
{
if (first.getNumerator()*second.getDenominator() <=
first.getDenominator()*second.getNumerator()){
return first;
}
else {
return second;
}
}
}
You must use the Faction(int, int) constructor instead of Fraction() constructor:
Fraction frac = new Fraction ();
must be
Fraction frac = null;
//later in the code once you have a and b variables set and no exceptions...
frac = new Fraction (a, b);
OR use the setters:
frac.setNumerator(a);
frac.setDenominator(b);
before returning frac variable.
Looks like you are not setting the values you read from the console
a = getNumber();
b = getNumber();
frac.setNumerator(a);
frac.setDenominator(b);
by default a and b are set to 1, when you say
Fraction frac = new Fraction();
because the Fraction default constructor is defined like this
public Fraction()
{
a = 1;
b = 1;
}
In porting an algorithm from JavaScript to Java, I've run into the problem that I need a replacement for JavaScript's toPrecision(). The problem is that I don't have a clue how small or large the numbers will be, so I can't use a simple NumberFormat with the right format.
Is there a standard class that offers a similar functionality?
EDIT
Here is what I came up with:
double toPrecision(double n, double p) {
if (n==0) return 0;
double e = Math.floor(Math.log10(Math.abs(n)));
double f = Math.exp((e-p+1)*Math.log(10));
return Math.round(n/f)*f;
}
In principle, it does the right thing, but rounding errors completely ruin it. For example,
toPrecision(12.34567, 3) returns 12.299999999999997
EDIT 2
This version works perfectly for 11 out of 12 test cases...
double toPrecision(double n, double p) {
if (n==0) return 0;
double e = Math.floor(Math.log10(Math.abs(n)));
double f = Math.round(Math.exp((Math.abs(e-p+1))*Math.log(10)));
if (e-p+1<0) {
f = 1/f;
}
return Math.round(n/f)*f;
}
But toPrecision(0.00001234567, 3) still returns 1.2299999999999999E-5 instead of 1.23E-5
Use BigDecimal and setScale() method to set the precision
BigDecimal bd = new BigDecimal("1.23456789");
System.out.println(bd.setScale(3,BigDecimal.ROUND_HALF_UP));
Output
1.235
See
IDEone demo
The simplest solution I came up with for this uses a combination of java.math.BigDecimal and java.math.MathContext like so.
String toPrecision(double number, int precision) {
return new BigDecimal(number, new MathContext(precision)).toString();
}
I'm using this in the dynjs implementation of Number.prototype.toPrecision.
Here's a java solution using String.format.
public static String toPrecision(double d, int digits) {
s = String.format("%."+((digits>0)?digits:16)+"g",d).replace("e+0","e+").replace("e-0","e-");
return s;
}
The .replace is only needed if you want to mimic javascript where it has no leading zero on exponents. If you are just using it for a rounding then return the value as
return Double.parseDouble(s);
Here is some unit test code:
public void testToPrecision() {
String s = NumberFormat.toPrecision(1234567.0,5);
assertEquals("1.2346e+6",s);
s = NumberFormat.toPrecision(12.34567,5);
assertEquals("12.346",s);
s = NumberFormat.toPrecision(0.1234567,5);
assertEquals("0.12346",s);
s = NumberFormat.toPrecision(0.1234567e20,5);
assertEquals("1.2346e+19",s);
s = NumberFormat.toPrecision(-0.1234567e-8,5);
assertEquals("-1.2346e-9",s);
s = NumberFormat.toPrecision(1.0/3.0,5);
assertEquals("0.33333",s);
s = NumberFormat.toPrecision(1.0/3.0,0);
assertEquals("0.3333333333333333",s);
}
You can use double with
double d = 1.23456789;
System.out.println(Math.round(d * 1e3) / 1e3);
prints
1.235
or
System.out.printf("%.3f%n", d);
does the same.
public static void main(String... args) {
System.out.println(round3significant(12345678.9));
System.out.println(round3significant(0.0000012345));
}
public static double round3significant(double d) {
if (d < 100) {
double divide = 1;
while(d < 100) {
d *= 10;
divide *= 10;
}
return Math.round(d) / divide;
} else {
double multi = 1;
while(d > 1000) {
d /= 10;
multi *= 10;
}
return Math.round(d) * multi;
}
}
prints
1.23E7
1.23E-6
You can use NumberFormat to only display as a decimal.
This finally works...
double toPrecision(double n, double p) {
if (n==0) return 0;
double e = Math.floor(Math.log10(Math.abs(n)));
double f = Math.round(Math.exp((Math.abs(e-p+1))*Math.log(10)));
if (e-p+1<0) {
return Math.round(n*f)/f;
}
return Math.round(n/f)*f;
}
import java.text.*;
Class Decimals
{
public static void main(String[] args)
{
float f = 125.069f;
DecimalFormat form = new DecimalFormat("#.##");
System.out.println(form.format(f));
}
}
.## represents upto what decimal places you want
I hope this suits your requirement.
How do I find out the length or the number of digits of the fraction part of a decimal number?
I can see a few aproaches, e.g. with Strings like this one:
public static int getNumberOfFractionDigits(Number number) {
Double fractionPart = number.doubleValue() - number.longValue();
return fractionPart.toString().length() - 2;
}
But what is the best way to determine the length?
I could imagine some problems if I use Strings, e.g. because the locale and number format may be different from system to system. Is there a nice way to calculate it? Maybe without iteration?
Thanks in advance.
Try this:
public static int getNumberOfFractionDigits(Number number) {
if( number == null ) return 0; //or throw
if( number.doubleValue() == 0.0d ) return 0;
BigDecimal bd = new BigDecimal(number.toString());
//BigDecimal bd = BigDecimal.valueOf(number.doubleValue()); // if double precision is ok, just note that you should use BigDecimal.valueOf(double) rather than new BigDecimal(double) due to precision bugs in the latter
bd = bd.stripTrailingZeros(); //convert 1.00 to 1 -> scale will now be 0, except for 0.0 where this doesn't work
return bd.scale();
}
Edit:
If the number is actually an iteger (i.e. fraction of 0) this would still return 1. Thus you might check whether there actually is a fractional part first.
Edit2:
stripTrailingZeros() seems to do the trick, except for 0.0d. Updated the code accordingly.
I just wrote a simple method for this, hope it can help someone.
public static int getFractionDigitsCount(double d) {
if (d >= 1) { //we only need the fraction digits
d = d - (long) d;
}
if (d == 0) { //nothing to count
return 0;
}
d *= 10; //shifts 1 digit to left
int count = 1;
while (d - (long) d != 0) { //keeps shifting until there are no more fractions
d *= 10;
count++;
}
return count;
}
You may use java.text.NumberFormat.
nf = java.text.NumberFormat.getInstance ();
// without BigDecimal, you will reach the limit far before 100
nf.setMaximumFractionDigits (100);
String s = nf.format (number.doubleValue ())
You may set the Decimal-Identifier as you like, and use regular expressions to cut off the leading part, and String.length () to evaluate the rest.
Looks like many offered the Big Decimal. It's easy on the eyes at least.
The code shall work for ya.
package t1;
import java.math.*;
public class ScaleZ {
private static final int MAX_PRECISION = 10;
private static final MathContext mc = new MathContext(MAX_PRECISION, RoundingMode.HALF_EVEN);
public static int getScale(double v){
if (v!=v || v == Double.POSITIVE_INFINITY || v == Double.NEGATIVE_INFINITY)
return 0;//throw exception or return any other stuff
BigDecimal d = new BigDecimal(v, mc);
return Math.max(0, d.stripTrailingZeros().scale());
}
public static void main(String[] args) {
test(0.0);
test(1000d);
test(1d/3);
test(Math.PI);
test(1.244e7);
test(1e11);
}
private static void test(double d) {
System.out.printf("%20s digits %d%n", d, getScale(d));
}
}
That was my best implementation:
public class NumberHandler {
private static NumberFormat formatter = NumberFormat.getInstance(Locale.ENGLISH);
static {
formatter.setMaximumFractionDigits(Integer.MAX_VALUE);
formatter.setGroupingUsed(false);
}
public static int getFractionLength(double doubleNumber) {
String numberStr = formatter.format(doubleNumber);
int dotIndex = numberStr.indexOf(".");
if (dotIndex < 0) {
return 0;
} else {
return numberStr.length() - (dotIndex + 1);
}
}
}
Not effective, probably not perfect, but the other options was worse.
public static int getNumberOfFractionDigits(double d) {
String s = Double.toString(d), afterDecimal="";
afterDecimal = s.subString(s.indexOf(".") + 1, s.length() - 1);
return afterDecimal.length();
}