0

I am working on a application in which I am generating some Tickets using calculations. For Generating Tickets I have created a model class and Inserting values into appropriate object. Now I am having a Object

@property (nonatomic,assign) float crv;

I am adding a value into this from my textfiled like this :

ticketitem.crv = [txtCRV.text floatValue];

ticketitem is my Model class object. Now my

txtCRV.text is 1.58 but ticketitem.crv becomes 1.58000004

I have tried all possible ways like rounded, Converting into string and then float and so on. but nothing seems to be working I want ticketitem.crv == 1.58 only as it as my textfield value. Can anyone help?

Niharika
  • 1,188
  • 15
  • 37
  • Please have a look at [Is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – vadian Nov 08 '17 at 10:17
  • Possible duplicate of [Make a float only show two decimal places](https://stackoverflow.com/questions/560517/make-a-float-only-show-two-decimal-places) – Pratik Jamariya Nov 11 '17 at 09:37

3 Answers3

0

Try this

float rounded = roundf([txtCRV.text floatValue] * 100) / 100.0;

Let me know if you face problem using it.

Umang Loriya
  • 840
  • 8
  • 15
0

Replace ur code with this line

ticketitem.crv = [NSString stringWithFormat:@" %.2f", txtCRV.text];
Vamsi S
  • 269
  • 3
  • 16
0

try:

%f = 25.000000

%.f = 25

%.02f = 25.00

ticketitem.crv = [[NSString stringWithFormat:@"%.02f", [txtCRV.text floatValue]]floatValue];