I have the following code which works when I am multiplying whole numbers however when I try to multiply decimal numbers and display the answer as a decimal number I am getting an error

```
public void actionPerformed(ActionEvent e) {
int num1=Integer.parseInt(textArea_price.getText());
int num2=Integer.parseInt(textArea_quantity.getText());
int result = num1*num2;
textField_Name.setText(Integer.toString(result));
```

Any suggestions on what I can change or add to make this code work with decimal numbers is appreciated.

If you are expecting real numbers in the text fields then use double rather than int.

```
double num1 = Double.parseDouble(textArea_price.getText());
```

Change `int/Integer`

to `double/Double`

.

Note that `double`

is imprecise (for example `1.2 * 9`

gives `10.799999999`

when it should be `10.8`

). Consider using `BigDecimal`

if exactness is required.

Use `Double.parseDouble()`

instead of `Integer.parseInt()`

. And use `double`

type of variables:

`double num1 = Double.parseDouble(textArea.getText());`

BigDecimal could be what you want.

```
textField_Name.setText(
new BigDecimal(textArea_price.getText())
.multiply(new BigDecimal(textArea_quantity.getText())).toString());
```

My guess is that you're using the term "decimal number" to (erroneously) refer to numbers containing a "decimal point". Such numbers are not integers and cannot be parsed by Integer.toString. They need to be handled as either floating point (float or double) or as BigDecimal.

```
float a = Float.parseFloat("121.12");
float b = Float.parseFloat("0.9");
float res = a*b;
String str = Float.toString(res);
```

Similar Questions

I have this question about decimal numbers from oracle to php. If i have 0,7 on oracle, for example, when i use it on php, it will be ,7, without the zero. Anyone one knows why? And a possible sol

I've been told this : Computers convert binary data into the hexadecimal (hex) number system because it is much less complex than converting data into decimal numbers. Why it is less complex?

I am supposed to write and algorithm which uses recursion (Divide-And-Conquer) to multiply two arrays.These arrays hold big Numbers that are greater than long(int 64) or double capacity. Would please

I am trying to convert binary to decimal in the MIPS language, using the MARS simulator. The program accepts a binary number, and then does the conversion, by multiplying (shift left by the number's p

Is there a way to force Rails to output all decimal numbers with a certain precision? All of the decimal fields in my database are currency amounts, so I'd like decimal numbers to show by default with

I want to convert double precision numbers to decimal . for example I have 23.43, the double value would be (using matlab) : 174 71 255 122 20 110 55 64 now having this how can I write a java program

Is there some easy way to restrict TextInput to accept decimal numbers in range -99.999999 to +99.999999 ? it is Spark TextInput.

I want to check the sequential order of decimal numbers and find the missing number. For eg: If i have 1.1.1, 1.1.3, 1.1.4, 2.1.1, 2.1.3, 2.1.2, 3, etc Here i need to find the missing number 1.1.2 and

From my database I get a decimal like, 25.30. In PHP I multiply the decimal like this, 25.30 * 6. The result is 151,8 but with a comma. The problem is that the result gets me a comma instead of a poin

I am trying to scale my number calculation to one decimal place (with all applicable rounding) using the bc command in linux. This is my test expression: echo scale 1; 90 - 70.333 | bc However, I en

INTRODUCTION AND RELEVANT INFORMATION: I have an edit control that should accept only signed decimal numbers-something like -123.456. Also, it should be locale aware, since decimal separator is not th

In Java BigDecimal class contains values as A*pow(10,B) where A is 2's complement which non-fix bit length and B is 32bit integer. In C# Decimal contains values as pow (–1,s) × c × pow(10,-e) where th

I am trying to compare two decimal values in Java script. I have two objects as one assogned value 3 and the other as 3.00. When i say if (obj1 == obj2) it does not pass the condition as it is doe

This question already has an answer here: Why does (360 / 24) / 60 = 0 … in Java 3 answers How can i convert Integer value to decimal value? 3 answers Using Java i am getting a weird result

I noticed a strange behavior when multiplying decimal values in C#. Consider the following multiplication operations: 1.1111111111111111111111111111m * 1m = 1.1111111111111111111111111111 // OK 1.1111

I am trying to keep two decimal places, even if then numbers are zeroes, using DecimalFormatter: DecimalFormat df = new DecimalFormat(#.00); m_interest = Double.valueOf(df.format(m_principal * m_int

I created a file which looks like where the first column is the color line in decimal and the second column is y-axis. The x-axis is the row number. 0 0 1 1 2 2 ... Then I run this command plot test

I would like to fetch some decimal numbers from a string which I receive from a remote service. My issue is that I only want decimals in the string prefixed with either + or -. Here is my current

float f = 0.00f; System.out.println(f); gives the output: 0.00 I'd like to format a number represented by a percentage to 2 decimal places. But the result should be a float and not a string. e.g.

I want to know what a regex would look like for: only whole numbers only numbers with less than or equal to two decimal places (23, 23.3, 23.43)

hi I want to know how it is possible to convert a hexadecimal negative value (to complement encoding) to decimal, easily without converting hexadecimal to binary and then multiplying each bit in by a

I currently use the following code to print a double: return String.format(%.2f, someDouble); This works well, except that Java uses my Locale's decimal separator (a comma) while I would like to us

I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places. #include <iostream.h> #include <cstdlib.h> #incl

Anyone know of a java library that can take very large spelled numbers and convert them into digits? It'd be nice if it could do decimal places as well. Example.. Ten to 10

Can someone tell me what is the best way to multiply a series of numbers as a matrix? I mean. I have seen algorithms for matrix multiplication, but are to multiply numbers as matrix1 [4] [4] and matri

Is there a simple way that i can manipulate high precision decimal numbers in java, without a limit on the number of decimal places, and the ability to output the number in a println or write it to a

I have a problem with decimal numbers. If I use .(dot) instead of ,(comma) in the textbox it comes null in controller. I know its a language issue because in spanish we use comma instead of dot for de

I am trying to limit an input to a decimal number. I'd like any invalid characters not to be displayed at all (not displayed and then removed). I already have this implemented but for whole integers (

is there something wrong with the declaration of the decimal numbers. I wanted to perform decimal addition and subtraction in COBOL. http://www.compileonline.com/compile_cobol_online.php--> i used

I'm new to Java and trying to take a BigDecimal (for example 99999999.99) and convert it to a string but without the decimal place and trailing numbers. Also, I don't want commas in the number and rou

I need to represent some numbers in Java with perfect precision and fixed number of decimal points after decimal point; after that decimal point, I don't care. (More concretely - money and percentages

Can you guys help with this please? I need to convert a real number, stored as (x.y) into a decimal value in visual basic, and vice versa. Any psudocode/code/answers on the process would be great, che

I am Creating a Graph Plotting Software in java. As a part of process if the equation is of form of y = Sin(x) * Cos(x) then in that case the value of y is a decimal valued points. But in Java we are

This question already has an answer here: JavaScript displaying a float to 2 decimal places 3 answers So I made this quick script that calculates some numbers, but the answer ends with many dec

I wrote a program and I can't figure out how i should do the decimal format? I thought i was doing it right, apparently i can't do decimal format correctly. Can, someone help me with the decimal forma

If I want to represent some decimal numbers by using custom floating-point binary, how to efficiently find the minimum length of mantissa and exponent that will maintain precision needed to distinguis

This is exactly the same question as How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger?, except it applies to char[] rather than byte[]:

Is it worth it to implement it in hardware? If yes why? If not why not? Sorry I thought it is clear that I am talking about Decimal Rational Numbers! Ok something like decNumber++ for C++, decimal f

I have a number of decimal numbers stored in a database and need to calculate the percentage increase(or decrease) between two of the numbers using PHP. An example of two of the numbers are: 111.0516

Lets say i have an user input. The class standing has a total of 80 and the exam is 100. But how do i round the result off to two decimal places? System.out.print(Grades = 50% Class Standing + 50% Ex

In an attempt to see if I can clean up some of my math code, mostly matrix stuff, I am trying to use some Java Generics. I have the following method: private <T> T[][] zeroMatrix(int row, int co

I need help for a starting point, really. We must use these 3 loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers, in the ran

In the code below, I am getting a java.lang.NumberFormatException when entering large numbers ~1000000000. The error occurs on the second-last line -\ int integer = Integer.parseInt(split[0]); It sho

Im parsing xml using php and one of my variables receive a long decimal value. $ctr = 0.00529440333938; But I need to make this number as 0.52%, so I tried multiplying by 100 and forcing just 2 decim

In Java, all numeric types extend from java.lang.Number. Would it be a good idea to have a method like the following: public boolean areEqual(Number first, Number second) { if (first != null &&

I have assigned an integer to a double variable, but cout prints the double variable as an int. not as double . If I introduce cout << showpoint; in the code then I m able to see the decimal val

I want to round of my double to 3 decimal places in java. I don't want to trim off the zero. So if my double is 2.34, I still want it as 2.340.

So after finally learning how to store user input into a variable, I am realizing it is rather difficult to have the user input numbers with decimals. The Number KeyPad doesn't allow a decimal part, a

I have written a program to convert Decimal to Hexadecimal, which is equivalent of hex() function in python. The program prints correct value till '2559'. How to get correct values of hexadecimal for

In my application I want to input numbers (amounts) to a specific limit, and hence have used JFormattedTextField. Limit like 12345678.99 i.e. 8 digits before . and 2 after . or so on. This is my