Android - Float - Always 00 after point [duplicate]
This question already has an answer here:
Java integer-double division confusion [duplicate]
5 answers
I want to display the average number in the textview.
however, the output is always the integer.
For example, my input num1 is 2 and 3. the output is 2.00.
input: 2 and 7, the output is 4.00.
here is my code.
<string name="ave">Average: %1$.02f</string> //in strings.xml
//in main function
int num1 = Integer.parseInt(number1.getText().toString());
int num2 = Integer.parseInt(number2.getText().toString());
//get the average number
float average = ( num1 + num2 ) / 2;
ave_num.setText(getString(R.string.ave, average));
java android
marked as duplicate by Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Java integer-double division confusion [duplicate]
5 answers
I want to display the average number in the textview.
however, the output is always the integer.
For example, my input num1 is 2 and 3. the output is 2.00.
input: 2 and 7, the output is 4.00.
here is my code.
<string name="ave">Average: %1$.02f</string> //in strings.xml
//in main function
int num1 = Integer.parseInt(number1.getText().toString());
int num2 = Integer.parseInt(number2.getText().toString());
//get the average number
float average = ( num1 + num2 ) / 2;
ave_num.setText(getString(R.string.ave, average));
java android
marked as duplicate by Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Java integer-double division confusion [duplicate]
5 answers
I want to display the average number in the textview.
however, the output is always the integer.
For example, my input num1 is 2 and 3. the output is 2.00.
input: 2 and 7, the output is 4.00.
here is my code.
<string name="ave">Average: %1$.02f</string> //in strings.xml
//in main function
int num1 = Integer.parseInt(number1.getText().toString());
int num2 = Integer.parseInt(number2.getText().toString());
//get the average number
float average = ( num1 + num2 ) / 2;
ave_num.setText(getString(R.string.ave, average));
java android
This question already has an answer here:
Java integer-double division confusion [duplicate]
5 answers
I want to display the average number in the textview.
however, the output is always the integer.
For example, my input num1 is 2 and 3. the output is 2.00.
input: 2 and 7, the output is 4.00.
here is my code.
<string name="ave">Average: %1$.02f</string> //in strings.xml
//in main function
int num1 = Integer.parseInt(number1.getText().toString());
int num2 = Integer.parseInt(number2.getText().toString());
//get the average number
float average = ( num1 + num2 ) / 2;
ave_num.setText(getString(R.string.ave, average));
This question already has an answer here:
Java integer-double division confusion [duplicate]
5 answers
java android
java android
asked Nov 26 '18 at 12:07
田睿霖田睿霖
102
102
marked as duplicate by Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
The right side of this assignment:
float average = (num1 + num2) / 2;
consists only of integers.
So after the addition, the division will give the result of integer division.
This is how the compiler treats mathematical operations between integers.
Of course you want the result as float
.
So do this:
float average = 1.0f * ( num1 + num2 ) / 2;
By multiplying with 1.0f
(the f
suffix denotes float
) you will get the desired float
result.
Or:
float average = ( num1 + num2 ) / 2f;
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
add a comment |
Use DecimalFormat
float average = ( num1 + num2 ) / 2;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(average ));
or String.format
String average=String.format("%.02f", average )
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
add a comment |
I've already tested this code:
float average = ( num1 + num2 ) / 2f;
add a comment |
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
or
String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03
String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003
String.format("Value of a: %02d", a) // for 2 didgit ex. 01
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The right side of this assignment:
float average = (num1 + num2) / 2;
consists only of integers.
So after the addition, the division will give the result of integer division.
This is how the compiler treats mathematical operations between integers.
Of course you want the result as float
.
So do this:
float average = 1.0f * ( num1 + num2 ) / 2;
By multiplying with 1.0f
(the f
suffix denotes float
) you will get the desired float
result.
Or:
float average = ( num1 + num2 ) / 2f;
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
add a comment |
The right side of this assignment:
float average = (num1 + num2) / 2;
consists only of integers.
So after the addition, the division will give the result of integer division.
This is how the compiler treats mathematical operations between integers.
Of course you want the result as float
.
So do this:
float average = 1.0f * ( num1 + num2 ) / 2;
By multiplying with 1.0f
(the f
suffix denotes float
) you will get the desired float
result.
Or:
float average = ( num1 + num2 ) / 2f;
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
add a comment |
The right side of this assignment:
float average = (num1 + num2) / 2;
consists only of integers.
So after the addition, the division will give the result of integer division.
This is how the compiler treats mathematical operations between integers.
Of course you want the result as float
.
So do this:
float average = 1.0f * ( num1 + num2 ) / 2;
By multiplying with 1.0f
(the f
suffix denotes float
) you will get the desired float
result.
Or:
float average = ( num1 + num2 ) / 2f;
The right side of this assignment:
float average = (num1 + num2) / 2;
consists only of integers.
So after the addition, the division will give the result of integer division.
This is how the compiler treats mathematical operations between integers.
Of course you want the result as float
.
So do this:
float average = 1.0f * ( num1 + num2 ) / 2;
By multiplying with 1.0f
(the f
suffix denotes float
) you will get the desired float
result.
Or:
float average = ( num1 + num2 ) / 2f;
edited Nov 26 '18 at 12:29
answered Nov 26 '18 at 12:22
forpasforpas
19.3k4829
19.3k4829
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
add a comment |
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
1.0f* is work! thanks a lot!
– 田睿霖
Nov 27 '18 at 2:07
add a comment |
Use DecimalFormat
float average = ( num1 + num2 ) / 2;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(average ));
or String.format
String average=String.format("%.02f", average )
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
add a comment |
Use DecimalFormat
float average = ( num1 + num2 ) / 2;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(average ));
or String.format
String average=String.format("%.02f", average )
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
add a comment |
Use DecimalFormat
float average = ( num1 + num2 ) / 2;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(average ));
or String.format
String average=String.format("%.02f", average )
Use DecimalFormat
float average = ( num1 + num2 ) / 2;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(average ));
or String.format
String average=String.format("%.02f", average )
answered Nov 26 '18 at 12:10
sasikumarsasikumar
7,82511227
7,82511227
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
add a comment |
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:19
add a comment |
I've already tested this code:
float average = ( num1 + num2 ) / 2f;
add a comment |
I've already tested this code:
float average = ( num1 + num2 ) / 2f;
add a comment |
I've already tested this code:
float average = ( num1 + num2 ) / 2f;
I've already tested this code:
float average = ( num1 + num2 ) / 2f;
edited Nov 28 '18 at 10:12
Sreeram TP
3,32431441
3,32431441
answered Nov 26 '18 at 13:14
Hassan KazemyHassan Kazemy
1
1
add a comment |
add a comment |
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
or
String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03
String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003
String.format("Value of a: %02d", a) // for 2 didgit ex. 01
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
add a comment |
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
or
String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03
String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003
String.format("Value of a: %02d", a) // for 2 didgit ex. 01
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
add a comment |
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
or
String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03
String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003
String.format("Value of a: %02d", a) // for 2 didgit ex. 01
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
or
String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03
String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003
String.format("Value of a: %02d", a) // for 2 didgit ex. 01
answered Nov 26 '18 at 12:10
Vinesh ChauhanVinesh Chauhan
612120
612120
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
add a comment |
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
That won't help because the real problem is integer division, not the formatting
– Mark Rotteveel
Nov 28 '18 at 9:20
add a comment |