Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My input field BigDecimal num may contain both integer and floating numbers.
num = 45
or
num = 45.5343434
If the BigDecimal input field num contains decimal places, then I would like to limit the decimal places to 4.
Desired Output : 45.5343 if input num = 45.5343434
Desired Output : 45 if input num=45
How can I do that?
You are interested in what is called the scale of your BigDecimal.
Call BigDecimal#scale.
BigDecimal x = new BigDecimal( "45.5343434" );
BigDecimal y = new BigDecimal( "45" );
x.scale(): 7
y.scale(): 0
You asked:
I would like to limit the decimal places by 4
Test for the scale being larger than four. If so, round.
BigDecimal num = new BigDecimal(45);
if (num.scale() > 4) {
num = num.setScale(4, RoundingMode.HALF_UP);
}
System.out.println(num);
Output:
45
In case of more decimals:
BigDecimal num = new BigDecimal("45.5343434");
45.5343
You can choose a different rounding mode to fit with your requirements.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new to programming. I have a task to make a division of two arbitrary numbers, and to set arbitrary number of decimals. I was searching on the internet, but not really sure how to set it. If I could get some help, would much appreciate!
Here's the code so far:
int a,b, decimala;
System.out.println("first number: ");
a = unos.nextInt();
System.out.println("second number: ");
b = unos.nextInt();
System.out.println("amount of decimals: ");
decimala = unos.nextInt();
double c;
System.out.println(a);
System.out.println(b);
System.out.println("--------------");
c = (double)a/b;
System.out.println(%.decimala+ c);
If you just want to output them you could try using format
String format = "%" + decimala + "f";
System.out.format(format,a);
Here's a cheat sheet with all the stuff you can do.
https://alvinalexander.com/programming/printf-format-cheat-sheet
Thanks to #AndrewGuerra for pointing out how to format a variable amount of decimals
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have the following problem of a task that I need to make. It's about checking if a bankaccount number is correct or not this is the task:
The first group consists of 3 digits and determines which bank it is .
The second group consists of 7 digits and establishes the customer goes to that bank.
The third group consists of two numbers and a check digit which the validity of the bank account is determined .
The verification is done as follows:
The first and second group together form a number consisting of 10 digits. If you divide this number by 97 then the remainder after division by 97 must be equal to the third group of the bankaccount.
Maybe this is what you are looking for:
private boolean checkNumber(String number) {
//number consists of 12 digits
String firstGroup = number.substring(0, 3);
String secondGroup = number.substring(3, 10);
String thirdGroup = number.substring(10, 12);
int firstSecond = Integer.parseInt(firstGroup + secondGroup);
int third = Integer.parseInt(thirdGroup);
int remainderAfterDevision = firstSecond % 97;
return (remainderAfterDevision == third);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
First time using the DecimalFormat classes...
I need to store 4 decimals precision for my value.
Let me show you what I have done so far.
public class Paper
{
public String name, color, type,finish,grain;
public double width,height,gsm, lbs,ppi;
public DecimalFormat df;
public Paper()
{
String pattern = "#0.0000";
df = new DecimalFormat(pattern);
}
public void setWidthAndHeight(String width, String height)
{
//I've censured what I tried so far.
///I need to ensure that the passed width has 4 decimals and that it is stored that way.
this.width = //ENTER answer here! :)
}
Thank you for your time, and patience.
You can use BigDecimal to manage your decimals.
BigDecimal n = new BigDecimal("100.12345");
n = n.setScale(4, RoundingMode.HALF_UP);
n = n.stripTrailingZeros();
System.out.println(n.toPlainString());
Using scale will round your number to 4 decimal precision. Remove any zeros using stripTrailingZeros and use toPlainString to get your number later for printing.
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to find the sin of a number that the user inputs say for eg 1.5. I've done it on the calculator and it works but the code is not working.
Here is the code:
package msd1;
import java.util.Scanner;
public class Part3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
double Height = scanner.nextDouble();
double Angle = Height/2;
double asine = Math.asin(Angle);
System.out.println("Arcsine of " + Angle + " = " + asine);
}
}
Your variable names make no sense. A "height" is a length, and dividing a length by 2 doesn't give you an "angle". Furthermore, you don't pass an "angle" to asin, you pass it a number from -1 to +1 and it returns an angle.
In your case, you'd want to want to take the height of the ladder and divide it by its length to give you your sin (between -1 and +1), then take the asin of that value.
Odds are you also want to take the angle returned by asin in radians and convert to degrees.
You might have code like this:
double lengthOfLadder = 2.0;
double height = scanner.nextDouble();
double sine = height / lengthOfLadder;
double angleInRadians = Math.asin(sine);
double angleInDegrees = angleInRadians / Math.PI * 180;