Conflicts between toFixed() in JavaScript and DecimalFormat in Java - java

I have a number 10.625
In JavaScript
10.625.toFixed(2) //gives 10.63
In Java,
Double.valueOf(new DecimalFormat("###.##").format(Double.valueOf("10.625"))) //gives 10.62
How I can achieve unique output both at client and server side?

This will print 10.63, and the conversion back to Double will be based on this value.
DecimalFormat df = new DecimalFormat("###.##");
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(df.format(Double.valueOf("10.625")));

The way to harmonise the view of the number on client and server is to do your processing on the server and have the client display what the server sends it.
Use the Java version on the server, convert to a String, and send that to the client. Then you'll just be doing the conversion once, and won't have the possibility of a conflict.
After all, the client is just supposed to be giving a nice view of what the server has done. Repeating the calculations on the client is not the right way to think about it.

You could use Math.round(value * 100) / 100 for both java and javascript.

BigDecimal bd = new BigDecimal("10.625");
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
double value = Double.parseDouble(new DecimalFormat(".##").format(bd));
System.out.println(value);
will also print 10.63
Some important infos to catch :
since you never want to know how much digit before . then you can simpy use .##
On DecimalFormat ==> .## format will print 0.x0 to 0.x while '0.00' format will print 0.x0 to 0.x0 so i prefer the second format

Related

Bitcoin Java RPC client - cannot create raw transaction

I'm using the java implementation of Bitcoin RPC client.
When I'm calling the createRawTransaction with int type the raw transaction created as expected:
BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
new BigDecimal(1));
When I'm trying to use double value instead of int:
BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
new BigDecimal(1.2));
I'm receiving this error: invalid amount.
When I'm trying it by using bitcoin-cli, it works as expected.
NOTE: I;m working on local testnet blockchain
The output of:
System.out.println(new BigDecimal(1.2));
System.out.println(BigDecimal.valueOf(1.2));
Is:
1.1999999999999999555910790149937383830547332763671875
1.2
So the short answer is to use the preferred way to convert a double: BigDecimal.valueOf(1.2)
The long answer is that floatting numbers are complicated and double is an approximation for 1.2

Java DecimalFormat - incorrect number format in linux

I have a problem with parsing decimal number in Linux environment. When I parse in Windows, everything's all right.
Below, code snippet
String price;
DecimalFormat FORMATTER = (DecimalFormat)DecimalFormat.getInstance();
double customPrice = FORMATTER.parse(price).doubleValue();
And results
When price='9' Then customPrice='9.0' - it is ok
When price='1,00' Then customPrice='100.0' - it is wrong
When price='25,00' Then customPrice='2500.0' - it is wrong
Can you tell me what the problem is ?
Thanks
Read here here about locales, probably you're using en_US which causes the ',' symbol to separate groups of thousands.
you can use also
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator(',');
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);
to state your own sepeartors
You can change the decimal separator of DecimalFormat. The easiest way by using a NumberFormat with the desired locale, e.g.:
NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.GERMAN);
double customPrice = FORMATTER.parse(price).doubleValue();

Java NumberFormatException Legacy Code

I have a legacy program (java 1.4) running under Tomcat/Jboss, however, i have copy it to a new server (java 1.7) and it throws the following exception.
java.lang.NumberFormatException: For input string: "0000000000000000009011,00"
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
java.lang.Double.valueOf(Double.java:504)
java.lang.Double.<init>(Double.java:597)
Since i don't have access to the source, is there any way to fix this? i haven't try with java 1.4 for the moment
You schould look to jvm parameters on old server. Your language parameters will change comma to dot actually so you do not need any code changes.
I think its not due to java version. Replace comma(,) with "" or period(.)
For example conside a string : String str = "0000000000000000009011,00";
Now you can try -
double result = Double.valueOf(str.replace(",", "."));
or
double result = Double.valueOf(str.replace(",", ""));
Rather use NumberFormat for parsing:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("0000000000000000009011,00");
double d = number.doubleValue();
This way you can select the right locale for the number representation. In the example above I used France - they use a comma for the decimal point.

android java solve equations in string format

I need to solve string equations in an android app, e.g. "3 + 4*(5 - log(100))". I have tried to use BeanShell for this, unfortunately I have some problems with the integer/decimal numbers. When I enter
Interpreter interpreter = new Interpreter();
String res = "9223372036854775807D";
interpreter.eval("result = " + res);
res = interpreter.get("result").toString();
res = new BigDecimal(res).stripTrailingZeros().toPlainString();
I get as result 9223372036854776000??
But when I use String res = "9223372036854775807D"; I get the correct 9223372036854775807.
I simply cannot suspitude all D to L because then I get wrong results when having somthing like 3L/2L -> 1 (but should be 1.5
Does anyone know how to handle huge numbers such as 9223372036854775807 or -9223372036854775808 or can anyone suggest an alternative to BeanShell?
Use MathEval download it from this link: http://tech.dolhub.com/code/matheval
have you tried JEP expression parser?
it is a Good mathematical expression parser purely written in Java and can parse trigonometric,logarithm functions, complex values and you can customize your own functions also...

Format numbers in java

How do I get these formats in java?
Input:
1223893
180703
80967
1461
700
Output :
1,223,893
180,703
80,967
1,461
700
I will be always converting one by one number, this was just to get more examples.
you can read up on java number formatting here
so you would do something like this:
DecimalFormat myFormatter = new DecimalFormat('###,###,###');
String output = myFormatter.format('1223893');
if you output the output var it should have 1,223,893
Look for "grouping" and "thousands separator" here. DecimalFormatSymbols provides setGroupingSeparator(',') and you can set it on a DecimalFormat, together with setGroupingSize(3). To illustrate:
DecimalFormat df = new DecimalFormat();
df.getDecimalFormatSymbols().setGroupingSeparator(',');
df.setGroupingSize(3);
System.out.println(df.format(1223893)); // prints 1,223,893
You could use DecimalFormat.
Take a look at the DecimalFormat class.
google for NumberFormat in java
See the api docs.

Categories

Resources