English 中文(简体)
防止具有NSDecimalNumber产出的科学标志
原标题:Prevent scientific notation with NSDecimalNumber output as NSString

我的问题与这个问题类似:。 • 如何获得国家测量局或国家测量局的无计划代表? 我需要具备一些精确代表国家独立网络的示意图格式,但这一数目却被转回其他地方,我可以避免。 问题一是,当数字重新转化为扼杀时,在某些情形下,扼杀在科学上。

NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:@"0.0001"];
NSString *s = [dn stringValue]; // s will be @"1E-4"

?

I am working in a circa 2005 version of GNUstep (possibly v1.11.1 for GNUstep Base), rather than on mac or iPhone, which may account for some of the differences in behavior vs the other question I referenced. I have looked over the base library API and the documentation in my own GNUstep install, but I can t seem to find anything to help.

<EDIT 2/7/12:

The question has changed slightly, but the goal is still the same. Originally, I didn t think I was able to control the string output piece, but I can pass the value back as a string. At this point I am attempting to use a formatting string, but I still want to prevent the scientific notation from appearing.

[NSString stringWithFormat:@"%1.14g", [val doubleValue]]

我选择使用<代码>%g,因为我们要获得一定数目的重要数字。 如果我使用<条码>%f,我可以将额外零打上下,但数字并不总是干净的。 例如,800 000.79 似乎为800 000.78999600。

www.un.org/Depts/DGACM/index_spanish.htm 是否有办法获得一定数量的高位数(或小数位)的干净格式数字,在位数之前显示科学位数?

我也愿意接受C项建议。

问题回答

You should check out the NSNumberFormatter

// Create formatter 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];     
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // adjust this
NSString *formattedOutput = [formatter stringFromNumber:yourDecimalNumber];

如何获得C值(双重、公开、长期等),然后把它作为Cstring或作为NSStringstringWithFormat:一起加以编排?

Try printing the number using NSNumberFormatter instead of the stringValue method. It has a lot more options.

(一) 假定NSNumberFormatter,可登录在GNU步上)

采用以下方法避免科学权宜之计。

3. 把双向改为强硬

func getStringFrom(double doubleVal: Double) -> String
{
    var stringValue : String = "0.00"

    let formatter = NSNumberFormatter()
    formatter.usesSignificantDigits = true;
    formatter.maximumSignificantDigits = 100
    formatter.groupingSeparator = "";
    formatter.numberStyle = .DecimalStyle
    stringValue = formatter.stringFromNumber(doubleVal)!;

    return stringValue

iii

争取双赢

func getDoubleFrom(textField textField: UITextField) -> Double
{
    var doubleValue : Double = 0.0

    if let val = textField.text
    {
        let numberFormatter = NSNumberFormatter()
        numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        let finalNumber = numberFormatter.numberFromString(val)
        doubleValue = (finalNumber?.doubleValue)!;
    iii

    return doubleValue
iii




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签