json parse long double values without auto format - java

I'm trying to parse json array with some values.
Got some problems with long values like that "0.00009800".
I've tried jsonObject.getDouble and getString on it - it returns 9.8E-5.
Also tried BigDecimal with same results.
It should has simple solution which i'm missing.
tmp.high24h = Double.valueOf(e.getString("High"));
tmp.low24h = java.math.BigDecimal.valueOf(e.getDouble("Low")).doubleValue();

9.8E-5 is the expected value. It represents 9.8 * 10^(-5).

Always use String datatype for larger decimal values. After the conversion of json string to java object, convert it to BigDecimal value by passing the string as an argument in BigDecimal constructor.

Got it:
tmp.high24h = e.getDouble("High");
String test = String.format(Locale.US, "%.5f", tmp.high24h);

Related

How can I format a String number to have commas in Kotlin?

i have this code to calculate distance from 2 points(lat,lon)
val distance = distanceInKm(3.140853,21.422510,101.693207,39.826168)
val number3digits:Double = String.format("%-10.4f%n%n", distance).toDouble()
distancetomakkah.text = "$number3digits"
but i'm getting this output:
10891.3684
and i want the output to be like this:
10,891
i tried to change the format to:
("%-10.4f%n%n"),("%,d") and many others but i keep getting this error
f != java.lang.string
what am i doing wrong here?
String.format("%-10.4f%n%n", distance).toDouble()
Your format is not being applied. After formatting your returned value of distanceInKm as a String, you are calling toDouble, undoing any formatting you did.
distancetomakkah.text = "$number3digits"
number3digits is a Double, and default formatting is applied when you use string interpolation to assign .text.
Try
String.format("%-,10.4f%n%n", distance);
The result is of type String
That wouldn't be portable of course, since the , is not used in some Locales

JSONArray(jsonstring) drops decimal if 0

I have a json string with floating values:
{"foo":10.0,"bar":12.005}
I need to convert it using JSONObject(jsonstring) and I need to retain the decimals, but the json array drops them if they are zero. The result looks like
{"foo":10,"bar":12.005}
I expected that i could provide additional parameters to control the data type but according to
https://developer.android.com/reference/org/json/JSONObject
there is no such option. I also searched google and stackoverflow but i cannot find any similar problems.
JSONObject always treats everything as Objects so they must be converted to float by parsing it.
String json = "{\"foo\":10.0}";
try{
JSONObject jo = new JSONObject(json);
float f = Float.parseFloat(jo.get("foo").toString());
System.out.println(f);
}
catch(Exception e){
// Some parsing exception occurs
}
Hope this solves the issue.
Also JSONObject supports methods for getting the items in various datatype like double, int, boolean
double d = jo.getDouble("foo");
System.out.println(d); // gave me 10.0
Similarly we have
int i = getInt("name"); // which returns an integer
boolean b = getBoolean("name"); // Automatically parses
There's no way to get the number of digits from JSON.parse or eval. Even if IBM's decimal proposal had been adopted by the EcmaScript committee, the number is still going to be parsed to an IEEE 754 float.
Take a look a http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js for a simple JSON parser that you can modify to keep precision info.
solution already provided here
How to prevent removing decimal point when parsing JSON?
This is the expected behaviour.
The DECIMAL field is converted to the JSON NUMBER data type.
This type trims trailing zeros by default.
It's up to the client/receiver of the JSON to decide how many decimal places it needs to show and set the correct display format.
For the 10.0 it will add value as an integer in JSON, if you want it with decimal then first you need to convert it as a string and then you need to put string value in JSON.
val foo = 10.0
val bar = 12.005
val strFoo = foo.toString();
val jsonObject = JSONObject()
jsonObject.put("foo",strFoo)
jsonObject.put("bar", bar)
I guess, it's not a problem of JSON array, rather language's type conversion from float to int.
Use something similar to this to format float to string String.format("%,.2f", val);
Edit
The Workflow will go as follows:
if(Math.ceil(val)==val){ //a number with no fractional points
String str = String.format("%,.2f", val);
}
else //use_the_floating_number_as_usual

BigDecimal RoundingMode.HALF_UP doesn't work as expected [duplicate]

I have a BigDecimal defined like this:
private static final BigDecimal sd = new BigDecimal(0.7d);
if i print it, i get the value:
0.6999999999999999555910790149937383830547332763671875
which causes some wrong calculations. Does anyone know a way to get the exact value of 0.7 as BigDecimal? Change it to 0.71 would view the right result, but it shouldn't be like that
Use a String literal:
private static final BigDecimal sd = new BigDecimal("0.7");
If you use a double, actually public BigDecimal(double val) is called. The reason you do not get 0.7 is that it cannot be exactly represented by a double. See the linked JavaDoc for more information.
Perhaps if you bothered to read the documentation, i.e. the javadoc of the constructor you're using, you'd already know the answer.
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
When you then look at the javadoc of BigDecimal.valueOf(double), you'll find:
Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).
So there is your answer: Use BigDecimal.valueOf(0.7d), not new BigDecimal(0.7d).
You should use the declared value in String literal such as new BigDecimal("0.7");
Here are three ways:
private static final BigDecimal sd = new BigDecimal("0.7");
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL32);
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL64)
Constructing a BigDecimal from a double is surprisingly complicated. First, it can only be done via the detour of a string. (You can't get the constructor with double and a MathContext right. I've tried a lot. At the latest in cases in which the number of places before the decimal point would need to change due to rounding, it becomes difficult. Hence the warning in the Javadoc that you shouldn’t use it.)
However, even there, it is not enough with a simple String.format(), since String.format() is sensitive to the default Locale and outputs different decimal separators depending on system/VM settings, while the BigDecimal constructor always requires a dot as a decimal separator. So you have to construct your own Formatter with Locale.US. If you have this up and running, you will get a warning of an unclosed resource.
I found this to work:
static BigDecimal toBigDecimal(double value, int decimalPlaces) {
String format = "%." + decimalPlaces + "f";
try (Formatter formatter = new Formatter(Locale.US)) {
String formatted = formatter.format(format, value).toString();
return new BigDecimal(formatted);
}
}

BigDecimal adding wrong value

I have a BigDecimal defined like this:
private static final BigDecimal sd = new BigDecimal(0.7d);
if i print it, i get the value:
0.6999999999999999555910790149937383830547332763671875
which causes some wrong calculations. Does anyone know a way to get the exact value of 0.7 as BigDecimal? Change it to 0.71 would view the right result, but it shouldn't be like that
Use a String literal:
private static final BigDecimal sd = new BigDecimal("0.7");
If you use a double, actually public BigDecimal(double val) is called. The reason you do not get 0.7 is that it cannot be exactly represented by a double. See the linked JavaDoc for more information.
Perhaps if you bothered to read the documentation, i.e. the javadoc of the constructor you're using, you'd already know the answer.
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
When you then look at the javadoc of BigDecimal.valueOf(double), you'll find:
Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).
So there is your answer: Use BigDecimal.valueOf(0.7d), not new BigDecimal(0.7d).
You should use the declared value in String literal such as new BigDecimal("0.7");
Here are three ways:
private static final BigDecimal sd = new BigDecimal("0.7");
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL32);
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL64)
Constructing a BigDecimal from a double is surprisingly complicated. First, it can only be done via the detour of a string. (You can't get the constructor with double and a MathContext right. I've tried a lot. At the latest in cases in which the number of places before the decimal point would need to change due to rounding, it becomes difficult. Hence the warning in the Javadoc that you shouldn’t use it.)
However, even there, it is not enough with a simple String.format(), since String.format() is sensitive to the default Locale and outputs different decimal separators depending on system/VM settings, while the BigDecimal constructor always requires a dot as a decimal separator. So you have to construct your own Formatter with Locale.US. If you have this up and running, you will get a warning of an unclosed resource.
I found this to work:
static BigDecimal toBigDecimal(double value, int decimalPlaces) {
String format = "%." + decimalPlaces + "f";
try (Formatter formatter = new Formatter(Locale.US)) {
String formatted = formatter.format(format, value).toString();
return new BigDecimal(formatted);
}
}

Convert string to BigDecimal in java

I am reading a currency from XML into Java.
String currency = "135.69";
When I convert this to BigDecimal I get:
System.out.println(new BigDecimal(135.69));
Output:
135.68999999999999772626324556767940521240234375.
Why is it that it outputs this many numbers? How can I avoid this? All I want is for it to output 135.69.
The BigDecimal(double) constructor can have unpredictable behaviors. It is preferable to use BigDecimal(String) or BigDecimal.valueOf(double).
System.out.println(new BigDecimal(135.69)); //135.68999999999999772626324556767940521240234375
System.out.println(new BigDecimal("135.69")); // 135.69
System.out.println(BigDecimal.valueOf(135.69)); // 135.69
The documentation for BigDecimal(double) explains in detail:
The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a
BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with
a scale of 1), but it is actually equal to
0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that
matter, as a binary fraction of any finite length). Thus, the value
that is being passed in to the constructor is not exactly equal to
0.1, appearances notwithstanding.
The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which
is exactly equal to 0.1, as one would expect. Therefore, it is
generally recommended that the String constructor be used in
preference to this one.
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give
the same result as converting the double to a String using the
Double.toString(double) method and then using the BigDecimal(String)
constructor. To get that result, use the static valueOf(double)
method.
String currency = "135.69";
System.out.println(new BigDecimal(currency));
//will print 135.69
You are storing 135.69 as String in currency. But instead of passing variable currency, you are again passing 135.69(double value) into new BigDecimal().
So you are seeing a lot of numbers in the output.
If you pass the currency variable, your output will be 135.69
May I add something. If you are using currency you should use Scale(2), and you should probably figure out a round method.
BigDecimal b = BigDecimal.valueOf(d);
import java.math.*;
public class Test {
public static void main(String[] args)
{
// Creating a Double Object
Double d = new Double("785.254");
/// Assigning the bigdecimal value of ln to b
BigDecimal b = BigDecimal.valueOf(d);
// Displaying BigDecimal value
System.out.println("The Converted BigDecimal value is: " + b);
}
}
Spring Framework provides an excellent utils class for achieving this.
Util class : NumberUtils
String to BigDecimal conversion -
NumberUtils.parseNumber("135.00", BigDecimal.class);
Hi Guys you cant convert directly string to bigdecimal
you need to first convert it into long after that u will convert big decimal
String currency = "135.69";
Long rate1=Long.valueOf((currency ));
System.out.println(BigDecimal.valueOf(rate1));

Categories

Resources