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
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 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
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'm looking for a function such as Math.floor(int x) but for a variable of type BigInteger. Does anyone know how to do it?
Let's suppose a and b are int variables. In that case a/b is an int. Test case:
int a = 5;
int b = 2;
System.out.println(a / b); //expected output is 2
Let's suppose a and b are integers, but we intend to convert the result of the division into a float. The result depends on the way of conversion. Test case:
int a = 5;
int b = 2;
System.out.println((float)(a / b)); //expected output is 0.0
System.out.println(((float)a) / b); //expected output is 2.5
Let's suppose a and b are BigInteger variables. a.divide(b) will return a BigInteger value (so floor is not needed) unless b is 0 in which case it will throw an exception. Source.
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 7 years ago.
Improve this question
I have expression like below, and I need to parse them.
Examples:
Total volumes of A + Total volumes of B
Total sales of A / Total sales of B
Total Shipment of A - Total Shipment of B
When I get the string , I don't know which operation is present in the string. So using regex I want to parse each expression in java, and I should get this result.
expression1 = Total volumes of A
expression2 = Total volumes of B
operator = +
Can anyone help me with this. I want an efficient way to do this in java, there are 1000's of such expression
As #Pshemo stated, groups are the solution.
I did make the assumption that operator characters aren't part of the operands:
Pattern pattern = Pattern.compile("([\\w\\s]+)\\s+([+\\-/*])\\s+([\\w\\s]+)");
Matcher matcher = pattern.matcher("Total volumes of A + Total volumes of B");
if (matcher.matches()) {
System.out.println("operand 1: " + matcher.group(1));
System.out.println("operator: " + matcher.group(2));
System.out.println("operand 2: " + matcher.group(3));
}
Output:
operand 1: Total volumes of A
operator: +
operand 2: Total volumes of B
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