0

I am making an iOS calculator app in which the scientific notion is enabled by using

Label.text = [NSString stringWithFormat:@"%g",savedValue];

Its format is e.g. 1.09101e+120. Since I have enough display space I would like to make it more logical and display it as 1.09101x10120.
How can this be achieved?

Peter V
  • 2,478
  • 6
  • 36
  • 54

3 Answers3

3

You will probably need another label. You need to split your string in two parts (search for e+). Extract the number and put it in the other label. And you have to do the layout on your own.

Even easier, if Helvetica or any other Font on the iPhone supports these characters: ⁰¹²³⁴⁵⁶⁷⁸⁹ (as mentioned by Marcelo), you can just use them. But still you have to build your own custom string by replacing the e+ with x10 and the 120 with the mentioned characters.

calimarkus
  • 9,955
  • 2
  • 28
  • 48
1

I can't be bothered presenting an Objective-C solution, but here's one done in Python for inspiration:

>>> import re
>>> s = '1.09101e+120'
>>> (mantissa, expsign, exp) = re.match('^(.*)[Ee]([-+]?)(\d+)', s).groups()
>>> super = u''.join( u'⁰¹²³⁴⁵⁶⁷⁸⁹'[int(d)] for d in exp )
>>> print mantissa + u'×10' + ('-' if expsign == '-' else '') + super
1.09101×10¹²⁰
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

You can use Use a CATextLayer with an NSAttributedString(iOS 3.2 and above).

NSDictionary * superScriptAttribute = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:kCTSuperscriptAttributeName];  

Take a look at Bold & Non-Bold Text In A Single UILabel?

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144