0

I logged two NSDecimalNumber

NSLog(@"%@",[[NSDecimalNumber alloc] initWithFloat:2.675f]);
NSLog(@"%@",[[NSDecimalNumber alloc] initWithFloat:2.000f]);

I got the results in the console as

10:26 2013-05-16 10:23:20.807 SampleTest[1097:19d03] 2.674999952316284416
2013-05-16 10:23:22.484 SampleTest[1097:19d03] 2

My expected result is 2.0000. It is fine to have any number of zero's after decimal But should have atleast one zero after the decimal point. It will be hard for me to change the NSDecimalNumber datatype.

How can I acheive that. Can anyone help me out. Thanks in advance.

Aswathy Bose
  • 4,279
  • 4
  • 32
  • 44
  • is there any restrictions for using NSDecimalNumber?convert float into int and append 0's. – Balu May 16 '13 at 05:18
  • @Sunny : Yes, I need to convert float value to NSDecimalNumber Class, even if I append zero, NSDecimalNumber class is not giving me the precision(value after decimal point) – Aswathy Bose May 16 '13 at 05:25

1 Answers1

0

As decimal number is a represented in base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer

Therefore 0s after decimal gets truncated. If you want explicitly then you need to use float or double.

NSDecimalNumber *deciA=[NSDecimalNumber decimalNumberWithMantissa:2675 exponent:-3 isNegative:NO]; 
NSDecimalNumber *deciB=[NSDecimalNumber decimalNumberWithMantissa:2000 exponent:-3 isNegative:NO];

    NSLog(@"deciA is %@",deciA);
    NSLog(@"deciB is %@",deciB);

Output:

deciA is 2.675

desiB is 2

You can do this way:

NSDecimalNumber *deciA=[[NSDecimalNumber alloc] initWithFloat:2.675f];
NSDecimalNumber *deciB=[[NSDecimalNumber alloc] initWithFloat:2.000f];
NSLog(@"deciA is %.3lf",[deciA doubleValue]);
NSLog(@"deciB is %.3lf",[deciB doubleValue]);

Output:

deciA is 2.675

desiB is 2.000

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • I can get the value like this .But I want NSDecimalNumber with precision – Aswathy Bose May 16 '13 at 06:25
  • I have a DB , its dataformat is NSDecimalNumber.So I can't change the datatype – Aswathy Bose May 16 '13 at 06:38
  • @Pavitra: NSDecimalNumber represents a decimal number, but it does not care about the *output format*. **"2" and "2.0000" is exactly the same for NSDecimalNumber**. The output of NSDecimalNumber with `%@` gives just some representation. If you want to *print* the number with a given precision, you have to convert to double and use a `%f` output format (as Anoop said). Alternatively you can convert NSDecimalNumber to a string using NSNumberFormatter. – Martin R May 16 '13 at 06:57
  • @MartinR : I want to pass this value to the server as decimal (2.0). – Aswathy Bose May 16 '13 at 07:11
  • @Pavitra: your server will not be knowing about NSDecimal. That must be a Java based server that expects a float or decimal number. – Anoop Vaidya May 16 '13 at 07:16
  • @AnoopVaidya : Server is not aware of the value.We have to sent it from client side – Aswathy Bose May 16 '13 at 07:25