Print without the excess 0's in a floating point? [closed] - java

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
I understand that you can set the decimal places being printed for a float by doing this %.2f but I want to print only significant figures:
1.33443
1.3
2.00006

Use this
double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}

public static void main(String [] args) {
float f1 = 1.3344300f;
float f2 = 1.3000f;
float f3 = 2.010f;
System.out.println(f1);;
System.out.println(f2);;
System.out.println(f3);;
}
Always prints...
1.33443
1.3
2.01

Related

Arbitrary amount of decimals while dividing [closed]

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

Put point in string value (android java) [closed]

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

Function floor of BigInteger [closed]

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.

How to change 15% into 0.15 in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So, I am making a basic tip calculator and I need to know how to change the users input of 15%, 23%, etc. into 0.15, 0.23, etc.
This is my current code:
import java.util.Scanner;
public class TipCalc {
public static void main(String args[]) {
Scanner meal = new Scanner(System.in);
double food, tax, tip, fin;
System.out.println("How much was the meal?");
food = meal.nextDouble();
System.out.println("How much was the tax?");
tax = meal.nextDouble();
System.out.println("How much would you like to tip? I recomend 15%");
tip = meal.nextDouble();
}
}
Please help! Thanks!
double tip;
tip = meal.nextDouble() / 100;
Or
double tip = 15.0;
tip = tip / 100;
Or Even
double tip = 15.0;
tip /= 100;
If the input for your tip is 15.
Using
tip = meal.nextDouble() / 100.0;
System.out.println(tip);
would OUTPUT: 0.15

DecimalFormat Spaghetti [closed]

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

Categories

Resources