Solved

# COntvert and round off float

Posted on 2004-11-09
736 Views
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
Question by:894359

LVL 13

Expert Comment

float answC= Math.floor(numA/numB * 100);
0

Author Comment

That didn't work for me! Also I don't care if  answC holds the final value or not.
0

LVL 6

Accepted Solution

or you can declare ansWC as an int

int answC ;

answC = (int)(numA/numB * 100);
0

LVL 2

Expert Comment

BigDecimal answCdec=new BigDecimal(answC);
answC= answCdec.setScale(2).doubleValue();
0

LVL 2

Expert Comment

oops should be ...

BigDecimal answCdec=new BigDecimal(answC);
answC= answCdec.setScale(0).doubleValue();
0

LVL 32

Expert Comment

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

Author Comment

Thanks!
0

## Featured Post

### Suggested Solutions

In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!