【问题标题】:Changing the format of scientific notation in a calculator app在计算器应用程序中更改科学计数法的格式
【发布时间】:2012-08-27 12:22:36
【问题描述】:

我正在制作一个 iOS 计算器应用程序,其中通过使用启用科学概念

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

它的格式是例如1.09101e+120。由于我有足够的显示空间,我想让它更符合逻辑,并将其显示为 1.09101x10120
如何实现?

【问题讨论】:

    标签: objective-c ios xcode calculator


    【解决方案1】:

    您可能需要另一个标签。您需要将字符串分成两部分(搜索 e+)。提取数字并将其放在另一个标签中。而且你必须自己做布局。

    更简单的是,如果 iPhone 上的 Helvetica 或任何其他字体支持这些字符:⁰¹²³⁴⁵⁶⁷⁸⁹(如 Marcelo 所述),您就可以使用它们。但是您仍然必须通过将 e+ 替换为 x10 并将 120 替换为上述字符来构建自己的自定义字符串。

    【讨论】:

      【解决方案2】:

      我懒得介绍一个 Objective-C 解决方案,但这里是用 Python 完成的一个解决方案:

      >>> 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¹²⁰
      

      【讨论】:

        【解决方案3】:

        您可以使用 CATextLayerNSAttributedString(iOS 3.2 及更高版本)。

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

        看看Bold & Non-Bold Text In A Single UILabel?

        【讨论】:

        • 从 iOS 6 开始,您可以在 UITextField 中使用此权限。 (.attributedText)
        猜你喜欢
        • 2011-10-31
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-20
        • 2011-02-26
        • 2013-08-06
        相关资源
        最近更新 更多