5
I’m having a problem formatting decimals in my code in Objective-c, I would like to take advantage only 2 decimal places of my variable which is float
.
Ex: Value of the variable discount = 4.00043621
and I need it stored only 4.00
I could format only with StringWithFormat
, as the example below:
NSString* decimalFormatado = [NSString stringWithFormat: @"%.02f", discount];
thus the value of the variable decimalFormatado
was the way I wanted "4.00" however, in String
. and I need it to continue in float
, because I’ll need to use it as float
in a conditional structure further ahead.
Any suggestions for how best to do this?
I am looking for a way to enjoy only the 2 houses after the comma with a direct format, convert from string
for decimal
with the two houses I believe is not a good practice.
You speak in decimal but are using a
float
. Apparently you’re using it for things that involve money. Are you sure you want to do this? Don’t you think it’s strange that the value is4.00043621
? If you do an account with this, you know it will give unexpected results and they are usually considered wrong?– Maniero
I just realized friend, I will use rounding instead of truncate, thank you very much! reply there that already positive you!
– André Luiz
But if you still use the number for calculations just keep it in the original variable... For the formatted number normally declare as Nsstring in another variable as it is already being done..
– Daniel Omine