It's most likely rounding the 6 up to 7.
Why does NSNumberFormatter skip a number?
-
11-06-2023 - |
Pergunta
So lets say I have the following:
NSNumber *num = [NSNumber numberWithDouble:12345678901234567];
Now I would like for that to be formatted as a string in the following format: 1.234567E16
I have tried both:
formatter.numberStyle = NSNumberFormatterScientificStyle;
formatter.maximumFractionDigits = 6;
and
formatter.positiveFormat = @"0.00000E0";
But my result is always 1.23457E16
Why does it skip the 6?
If I change it to maximumFractionDigits = 7
it would print 1.234568E16
What am I doing wrong?
Solução
Outras dicas
I do not suggest using formatter.roundingMode = NSNumberFormatterRoundDown;
since that is a hack.
What you need to do to force 6 digits is set the maximum and the minimum number of fractional digits.
[formatter setMaximumFractionDigits:6];
[formatter setMinimumFractionDigits:6];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow