Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 759
  • Last Modified:

COntvert and round off float

HOw would I round off a float? For example:

float numA = 26.07;
float numB = 40.00;
float answC;

answC = numA/numB * 100;

I want to have answC hold 65 not 65.175
0
894359
Asked:
894359
1 Solution
 
petmagdyCommented:
      float answC= Math.floor(numA/numB * 100);
0
 
894359Author Commented:
That didn't work for me! Also I don't care if  answC holds the final value or not.
0
 
CodingExpertsCommented:
or you can declare ansWC as an int

int answC ;

answC = (int)(numA/numB * 100);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
siliconeagleCommented:
BigDecimal answCdec=new BigDecimal(answC);
answC= answCdec.setScale(2).doubleValue();
0
 
siliconeagleCommented:
oops should be ...

BigDecimal answCdec=new BigDecimal(answC);
answC= answCdec.setScale(0).doubleValue();
0
 
ldbkuttyCommented:
double numA = 26.07;
double numB = 40.00;
int answC;
answC = (int) Math.floor(numA/numB * 100);  // Note: return type is double. convert it to int (or float)
System.out.println("Answer C is ->" + answC);

If you want to declare the numbers as float, you should do:

float numA = (float) 26.07;  // value is double, convert it to float.
float numB = (float) 40.00; // value is double, convert it to float.
int answC;
answC = (int) Math.floor(numA/numB * 100);
System.out.println("Answer C is ->" + answC);
0
 
894359Author Commented:
Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now