How to display an integer value instead of a decimal value - java

I have this string:
$V{Formatter}.format(new Double($F{quantita}.replaceAll(",",".")))+" " + $F{unitaDiMisura}
that serves to display a number on a document, based on the value that the user digits on his control panel.
With this string, if the user digits "1", in the document appears "1,00".
What do I need to do if I don't want the decimals to be displayed?
Example: if the user digits "1", I want that in the document is displayed "1".
Sorry if something is not understandable, I'm not a developer...
Thanks in advance to everyone who will help.

You have to convert the double value to integer type.
I assume $F{quantita} contains the double value (in String) then,
String.valueOf(Double.valueOf($F{quantita}).intValue())
Above line can be split down like below.
Create a Double value from String.
Double value = Double.valueOf($F{quantita});
extract the Integer portion from double value.
Integer intValue = value.intValue();
Convert to String using toString().
intValue.toString();

Thanks guys! I was in need of the same exact thing :D I guess we are having troubles editing the same software (Fattura24).
I wanted to ask #Dinesh how would I then divide this integer by a set number?
E.g. I have $F{quantita} (which contains the double value if not converted to integer) and I need to display the result of its division by 4. So the output has to be the integer result of $F{quantita} /4. How can I achieve this? Thank you for your help

Related

Store large decimal number in android

In Android,
I going to store 8 digits in Sqlite but when i retrieved values from edittext to Float/Double then value was displaying with specific number format ie. "E" or some time add "." in values.
Edit text value is : 99989888
Result is : 9.998989
How i can resolve this issue?
I can't use BigDecimal because it's not supported by Sqlite and > operator.
You can try using long instead
You can use TEXT representation of BigDecimal and convert it in/out when you need it. Another aproach is here

Creating a class that accepts either a double or string in Java

I am trying to program (in Java) what seems like a relatively easy set of requirements but I am having trouble with types. I want to take scientific data from an instrument and put it through some logic to come up with an answer. My problem is that the data will either come off as a string (always "None") or a double. The data set will be two points per sample. So, here are my thoughts: I need class with two parameters that can be either a string or double. So, I decided I would make a class with generic parameters. When it came to performing the logic on the double or string, I had trouble. For example, here are two parts of the logic:
If v < 26 and f is between 36.5 and 37, then sample needs to be rerun.
If v > 31 and f is "None", then rerun.
There are more rules but I won't include them all here, but both v and f may be either a double or "None".
If all the values were doubles I would have no problem, but the fact that I can get doubles or strings is giving me trouble when trying to compare the two. I'm not sure if a generic class is the way to go. Any ideas?
Also, this is my first post so please let me know if you need more information or have any tips on posting.
Thanks!
You are mistaken: Your input type is always String (if it can be "None" it is a String). However, it can be a String with numbers and a dot that may be parsed as a Double.
Something like this should help:
public static Double parseInput(String input) {
try {
return input.equals("None") ? null : Double.valueOf(input);
} catch (NumberFormatException | NullPointerException e) {
return Double.NaN;
}
}
This returns a null for "None", which seems the most reasonable mapping, and handles anything not a number (blank, null, bad data) by returning the special "not a number" Double value.
I suggest you to use String as parameter type, then inside your class you can try to parse the value to double and apply your logic on it if the value can be parsed to double.
If not, then the value is a String and you have to apply the logic of String values
If all the values were doubles I would have no problem,
Similar to #Bohemian's answer but I would make use of NaN and leave the value a primitive.
public static double parseInput(String input) {
return "None".equalsIgnoreCase(input)
? Double.NaN
: Double.parseDouble(input);
}
To check for none use
If v > 31 and f is "None", then rerun.
if (v > 31 && Double.isNaN(f))

java.lang.NumberFormatException: Invalid int: "3546504756", what does this error mean?

I'm creating an Android app, and I'm reading some coordinates from a text file.
I'm using Integer.parseInt(xCoordinateStringFromFile) to convert the X coordinates to integers, and in the same way with the Y coordinates.
When I run the app, I get an error on that line, which looks like this:
BridgeData data = new BridgeData(
elements[0],
elements[1],
Integer.parseInt(elements[2]),
Integer.parseInt(elements[3]),
Integer.parseInt(elements[4]),
new GeoPos(Integer.parseInt(elements[5].split(",")[0]), Integer.parseInt(elements[5].split(",")[1])),
new GeoPos(Integer.parseInt(elements[6].split(",")[0]), Integer.parseInt(elements[6].split(",")[1])),
Integer.parseInt(elements[7]),
Integer.parseInt(elements[8])
);
The variable elements is a String array created by splitting the current line on every ;.
The "main" error is:
java.lang.NumberFormatException: Invalid int: "3546504756"
I wonder what this means, and how I can solve it.
Error just means that java is not able to convert the String that you are trying to use in your call to Integer.pasrseInt as that number is out of range of an integer.
You should be using Long.parseLong as 3546504756 number is out of range of an integer.
Make sure post that your BridgeData constructor accepts long as a parameter instead of integer.
Revising the concept of data type and their size might help you
http://www.tutorialspoint.com/java/java_basic_datatypes.htm
In Java, an int is 32 bits, which is enough to store numbers up to just over 2 billion. The number you were trying to read was an invalid int because it was too big.
I would seriously question the design of whatever you are doing, if you have coordinates with values of over a billion. But if you really need such big numbers, use long in place of int in your BridgeData class, and Long.parseLong in place of Integer.parseInt in the code that you quoted.
The range of int value can be lies between -2,147,483,648 to 2,147,483,647 and you are providing it more than that thats why it giving numberformatexception
You have to store the value in either long or other more range premetive type.
You can find more about java premetive data type range and value here

why do i have to use Integer.parseInt?

I am new to Java so forgive me if this is a silly question.
First I did the following (this is a section from my code):
public static void team()
{
int score = JOptionPane.showInputDialog("Enter the team score...");
calculate(score);
}
But I kept getting an error saying: "Incompatible types string cannot be converted to int".
So I thought I may need to use parsing. I did the following and it worked:
public static void team()
{
int myScore;
String score = JOptionPane.showInputDialog("Enter the team score...");
myScore = Integer.parseInt(score);
calculate(myScore);
}
I would like to know why there is a need for me to parse the variable "score"? In the first piece of code, I declared the variable score as an integer, so would it not store the input which I expect to be an Integer in the variable score which is then passed into the method calculate. I understand that parsing is when you convert a String into an Integer. However in this case, I do not expect the user to input a string so why is there a need for parsing?
The simple reason is: because JOptionPane.showInputDialog() returns a String, regardless of what you actually want the user to input.
There's no mechanism in Java to do automatic conversion based on the type of your destination variable myScore (though some other languages can do this, e.g. implicit conversions in Scala).
You could (in principle) create a custom dialog that returns an int , e.g. by getting the user to choose from a pulldown list of numbers, and then no parsing would be needed. But otherwise, you have to do parsing explicitly.
The JOptionPane.showInputDialog("Enter the team score..."); returns a String which you tried to store in a int. That is why you are getting the "incompatible types".
See Javadocs for more information.
Because a string is stored differently than an integer. Integers are stored in binary, while as a String is a collection of characters.
Example:
The number 14 would be stored as 1110 in Binary. However, as a String, it's stored as 00110001 00110100
Take a look at this http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp
Integer.parseInt() basically does the dirty work for you, by looking up the characters in a lookup table, and as long as they correlate with proper integer values, it coverts it over to binary form, to be stored in an integer.

Formatting a Double into the correct format

Having problem getting a Longitude/Latitude into the correct format for an api we don't have access to.
We need to convert this to a string and at the moment we have: 4.30044549E7
as the raw form but we need to convert it into 4.30044549 or similar (without Scientific Notation)
If we use
NumberFormat f = new DecimalFormat("#.#######");
f.format(4.30044549E7);
we get: 43004454.9
if we use
Double.toString(4.30044549E7);
we get: "4.30044549E7"
if we try to convert to an int we also get 43004454.9
Can anyone help? I can't find an acceptable solution to get rid of scientific notation on the number
If you really asked what I think you did, you could simply divide the number by 10 until you reach the format you wanted.
double value = 4.30044549E7;
while(value > 10 || value < -10){
value /= 10;
}
System.out.println(String.format("%.8f", value)); //4.30044549
Have you tried format?
String.format("%.10f",doubleValue);
It will format your number with 10 digits after dot.
1e-5 -> 0.000010000
1e5 -> 100000.0000000000
you never can "format" 4.30044549E7 into 4.30044549 because they are not the same.
4.30044549E7 is in fact 43004454.9 so wanting a formatter to display that double as 4.30044549 is an odd question

Categories

Resources