how to get sum between two decimal values in android studio
up vote
-2
down vote
favorite
I am working with an app android studio - mrate
is the rate for 1 km distance and replacetext
is the distance between two points and replacing the (km) form the distance - when distance value has decimal value (10.5 )then the system is getting an error
String mykm =dbDistance.getText().toString();
String mrate = dbRate.getText().toString();
System.out.println(mrate);
String replacetext;
replacetext = mykm.replace(" km", "");
System.out.println(replacetext);
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
//sum these two numbers
int z=x+y;
System.out.println(z);
android android-studio
add a comment |
up vote
-2
down vote
favorite
I am working with an app android studio - mrate
is the rate for 1 km distance and replacetext
is the distance between two points and replacing the (km) form the distance - when distance value has decimal value (10.5 )then the system is getting an error
String mykm =dbDistance.getText().toString();
String mrate = dbRate.getText().toString();
System.out.println(mrate);
String replacetext;
replacetext = mykm.replace(" km", "");
System.out.println(replacetext);
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
//sum these two numbers
int z=x+y;
System.out.println(z);
android android-studio
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
you obviously can't store decimal values inint
variable. As well as it is obvious that10.5
can't be parsed intoint
.
– Vladyslav Matviienko
Nov 8 at 9:04
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I am working with an app android studio - mrate
is the rate for 1 km distance and replacetext
is the distance between two points and replacing the (km) form the distance - when distance value has decimal value (10.5 )then the system is getting an error
String mykm =dbDistance.getText().toString();
String mrate = dbRate.getText().toString();
System.out.println(mrate);
String replacetext;
replacetext = mykm.replace(" km", "");
System.out.println(replacetext);
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
//sum these two numbers
int z=x+y;
System.out.println(z);
android android-studio
I am working with an app android studio - mrate
is the rate for 1 km distance and replacetext
is the distance between two points and replacing the (km) form the distance - when distance value has decimal value (10.5 )then the system is getting an error
String mykm =dbDistance.getText().toString();
String mrate = dbRate.getText().toString();
System.out.println(mrate);
String replacetext;
replacetext = mykm.replace(" km", "");
System.out.println(replacetext);
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
//sum these two numbers
int z=x+y;
System.out.println(z);
android android-studio
android android-studio
edited Nov 8 at 9:12
AS Mackay
1,6213816
1,6213816
asked Nov 8 at 8:48
Solahin Sourjah
32
32
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
you obviously can't store decimal values inint
variable. As well as it is obvious that10.5
can't be parsed intoint
.
– Vladyslav Matviienko
Nov 8 at 9:04
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10
add a comment |
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
you obviously can't store decimal values inint
variable. As well as it is obvious that10.5
can't be parsed intoint
.
– Vladyslav Matviienko
Nov 8 at 9:04
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
you obviously can't store decimal values in
int
variable. As well as it is obvious that 10.5
can't be parsed into int
.– Vladyslav Matviienko
Nov 8 at 9:04
you obviously can't store decimal values in
int
variable. As well as it is obvious that 10.5
can't be parsed into int
.– Vladyslav Matviienko
Nov 8 at 9:04
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can also use BigDecimal objects for easy conversion and arithmetics
BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();
BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general
add a comment |
up vote
1
down vote
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
If you've got decimal values, use Double
or Float
instead of Integer
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can also use BigDecimal objects for easy conversion and arithmetics
BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();
BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general
add a comment |
up vote
0
down vote
accepted
You can also use BigDecimal objects for easy conversion and arithmetics
BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();
BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can also use BigDecimal objects for easy conversion and arithmetics
BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();
BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general
You can also use BigDecimal objects for easy conversion and arithmetics
BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();
BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general
answered Nov 8 at 9:15
Brandon
11018
11018
add a comment |
add a comment |
up vote
1
down vote
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
If you've got decimal values, use Double
or Float
instead of Integer
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
add a comment |
up vote
1
down vote
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
If you've got decimal values, use Double
or Float
instead of Integer
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
add a comment |
up vote
1
down vote
up vote
1
down vote
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
If you've got decimal values, use Double
or Float
instead of Integer
//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);
If you've got decimal values, use Double
or Float
instead of Integer
answered Nov 8 at 8:56
Bruno
9261618
9261618
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
add a comment |
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
– Milos Lulic
Nov 8 at 9:01
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
Thank you very much. you save my day!
– Solahin Sourjah
Nov 8 at 9:08
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204212%2fhow-to-get-sum-between-two-decimal-values-in-android-studio%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50
2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at
– Solahin Sourjah
Nov 8 at 8:53
you obviously can't store decimal values in
int
variable. As well as it is obvious that10.5
can't be parsed intoint
.– Vladyslav Matviienko
Nov 8 at 9:04
NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10