将基本脚本转换为 Objective C(货币格式)

发布于 2024-09-13 03:30:32 字数 4160 浏览 4 评论 0原文

我有这个基本的类似脚本,我需要将其转换为 Objective C,它将大单位的金钱转换为缩短的版本(即:1.2m 等),我已经完成了大部分转换,但最大的问题是我我就在最后。

原始基本代码为:

; Basic Code

Function ShortCash$(BigNumber)
out$=""


    ; First, grab the length of the number
    L=Len(BigNumber)

    Letter$=""

    ;Next, Do a sweep of the values, and cut them down.
        If l<13 
            out$=(BigNumber/1000000000)

            ; For each figure, out remainder should be divided so that it leaves a 2 digit decimal number..
            remainder=(BigNumber Mod 1000000000)/10000000

            ; And we also want a letter to symbolise our large amounts..
            Letter$="b" ; BILLION!!!!
        EndIf
        If l<10 Then out$=(BigNumber/1000000):remainder=(BigNumber Mod 1000000)/10000:Letter$="m"
        If l<7 Then out$=(BigNumber/1000):remainder=(BigNumber Mod 1000)/10:Letter$="k"
        If l<4 Then out$=BigNumber:remainder=0:Letter$=""

    ;Next, if remainder=0 then we're happy..  ie, £1m is fine, we need no decimal.
    ;But, if the remainder is >0 we'll want a nice rounded 2 decimal number, instead.
    If remainder>0
        out$=out$+"."+Right$("00"+remainder,2) ; Last two numbers..

        ; Additionally, if the rightmost figure is a 0, remove it.
        ; (ie, if the value is 1.50, we don't need the 0)
        If Right$(out$,1)="0" Then out$=Left$(out$,Len(out$)-1)

    EndIf


    ; And throw on our letter, at the end.
    out$=out$+letter$ 

Return out$
End Function

// 以下内容由帖子作者于 8 月 5 日星期四编辑。

我相信我现在已经解决了,目前我已经为数千人提供了以下内容,我不确定它是否在所有情况下都有效,并欢迎对此提供任何帮助/指导。我知道内存问题,稍后我会解决这个问题,这是我首先解决的字符串操作部分。

// This goes inside the (IBAction) update method;

    NSNumber *bigNumber = nil;

    if ( [inputField.text length] >0)
    {
        bigNumber = [NSNumber numberWithInt:[inputField.text intValue]];
    }

    int bigNumberAsInt          = [bigNumber intValue];
    NSString *bigNumberAsString = [bigNumber stringValue];
    int bigNumberStrLen         = [bigNumberAsString length];

    NSLog(@"bigNumber = %@", bigNumber);
    //NSLog(@"bigNumberAsString = %@", bigNumberAsString);
    NSLog(@"bigNumberStrLen = %d", bigNumberStrLen);
    NSLog(@"=========");

    // =========

    NSNumberFormatter *nformat = [[[NSNumberFormatter alloc] init] autorelease];

    [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [nformat setCurrencySymbol:@"$"];
    [nformat setNumberStyle:NSNumberFormatterCurrencyStyle];
    [nformat setMaximumFractionDigits:0];

    NSLog(@"Cash = %@", [nformat stringFromNumber:bigNumber]);

    // =========


    NSString *output = [[NSString alloc] init]; 
    NSString *letter;


    // ==========

    // Anything less than 1m represent with a k
    if (bigNumberStrLen < 7) 
    {
        letter = @"k";
        int sum = (bigNumberAsInt / 1000);
        int int_remainder = ((bigNumberAsInt % 1000) / 10);

        NSLog(@"Remainder = %d", int_remainder);

        NSString *sumAsString = [NSString stringWithFormat:@"%d", sum];
        NSString *remainderAsString = [NSString stringWithFormat:@"%d", int_remainder];

        NSLog(@"Sum as String = %@", sumAsString);
        NSLog(@"Remainder as String = %@", remainderAsString);

        if (int_remainder >0)
        {
            NSLog(@"Remainder > 0");

            output = [output stringByAppendingString:sumAsString];
            output = [output stringByAppendingString:@"."];
            output = [output stringByAppendingString:remainderAsString];

            NSLog(@"Output = %@", output);

            NSUInteger outputStrLen = [output length];

            NSLog(@"Output strlen = %d", outputStrLen);

            if ([output hasSuffix:@"0"])
            {
                NSLog(@"Has suffix of 0");

                // Remove suffix
                output = [output substringWithRange: NSMakeRange(0, outputStrLen-1)];
            }

        }

        output = [output stringByAppendingString:letter];

        NSLog(@"Final output = %@", output);


    }

这将显示 10.2k(如果以 0 后缀结尾),或者将显示 10.2x,其中 X 是最后一个数字。

有人可以仔细检查一下吗,或者也许有一种更简单的方法来完成这一切。无论哪种情况,感谢您的帮助。

I've got this basic like script that I need to convert to objective c, it turns big units of money into shortened versions (ie: 1.2m, etc), I've got most of the conversion done, but the biggest problem I'm having is right at the end.

The original basic code is:

; Basic Code

Function ShortCash$(BigNumber)
out$=""


    ; First, grab the length of the number
    L=Len(BigNumber)

    Letter$=""

    ;Next, Do a sweep of the values, and cut them down.
        If l<13 
            out$=(BigNumber/1000000000)

            ; For each figure, out remainder should be divided so that it leaves a 2 digit decimal number..
            remainder=(BigNumber Mod 1000000000)/10000000

            ; And we also want a letter to symbolise our large amounts..
            Letter$="b" ; BILLION!!!!
        EndIf
        If l<10 Then out$=(BigNumber/1000000):remainder=(BigNumber Mod 1000000)/10000:Letter$="m"
        If l<7 Then out$=(BigNumber/1000):remainder=(BigNumber Mod 1000)/10:Letter$="k"
        If l<4 Then out$=BigNumber:remainder=0:Letter$=""

    ;Next, if remainder=0 then we're happy..  ie, £1m is fine, we need no decimal.
    ;But, if the remainder is >0 we'll want a nice rounded 2 decimal number, instead.
    If remainder>0
        out$=out$+"."+Right$("00"+remainder,2) ; Last two numbers..

        ; Additionally, if the rightmost figure is a 0, remove it.
        ; (ie, if the value is 1.50, we don't need the 0)
        If Right$(out$,1)="0" Then out$=Left$(out$,Len(out$)-1)

    EndIf


    ; And throw on our letter, at the end.
    out$=out$+letter$ 

Return out$
End Function

// The following was edited on Thur 5 Aug by Author of post.

I believe I've got it sorted now, I've got the following to work for thousands for the moment, I'm not sure if it will work under all circumstances and would welcome any help/guidance on this. I am aware of the memory issues, I'll sort that out later, its the string manipulation part I am resolving first.

// This goes inside the (IBAction) update method;

    NSNumber *bigNumber = nil;

    if ( [inputField.text length] >0)
    {
        bigNumber = [NSNumber numberWithInt:[inputField.text intValue]];
    }

    int bigNumberAsInt          = [bigNumber intValue];
    NSString *bigNumberAsString = [bigNumber stringValue];
    int bigNumberStrLen         = [bigNumberAsString length];

    NSLog(@"bigNumber = %@", bigNumber);
    //NSLog(@"bigNumberAsString = %@", bigNumberAsString);
    NSLog(@"bigNumberStrLen = %d", bigNumberStrLen);
    NSLog(@"=========");

    // =========

    NSNumberFormatter *nformat = [[[NSNumberFormatter alloc] init] autorelease];

    [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [nformat setCurrencySymbol:@"$"];
    [nformat setNumberStyle:NSNumberFormatterCurrencyStyle];
    [nformat setMaximumFractionDigits:0];

    NSLog(@"Cash = %@", [nformat stringFromNumber:bigNumber]);

    // =========


    NSString *output = [[NSString alloc] init]; 
    NSString *letter;


    // ==========

    // Anything less than 1m represent with a k
    if (bigNumberStrLen < 7) 
    {
        letter = @"k";
        int sum = (bigNumberAsInt / 1000);
        int int_remainder = ((bigNumberAsInt % 1000) / 10);

        NSLog(@"Remainder = %d", int_remainder);

        NSString *sumAsString = [NSString stringWithFormat:@"%d", sum];
        NSString *remainderAsString = [NSString stringWithFormat:@"%d", int_remainder];

        NSLog(@"Sum as String = %@", sumAsString);
        NSLog(@"Remainder as String = %@", remainderAsString);

        if (int_remainder >0)
        {
            NSLog(@"Remainder > 0");

            output = [output stringByAppendingString:sumAsString];
            output = [output stringByAppendingString:@"."];
            output = [output stringByAppendingString:remainderAsString];

            NSLog(@"Output = %@", output);

            NSUInteger outputStrLen = [output length];

            NSLog(@"Output strlen = %d", outputStrLen);

            if ([output hasSuffix:@"0"])
            {
                NSLog(@"Has suffix of 0");

                // Remove suffix
                output = [output substringWithRange: NSMakeRange(0, outputStrLen-1)];
            }

        }

        output = [output stringByAppendingString:letter];

        NSLog(@"Final output = %@", output);


    }

This will display 10.2k (if it ends with a 0 suffix) or it will display 10.2x where X is the last number.

Can someone just double check this, or perhaps there's an easier way to do all this. In either case, thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-09-20 03:30:32

为了改进解决方案,一个好主意可能是对 NSNumberFormatter 类进行子类化并重写 - (NSString *)stringForObjectValue:(id)anObject 方法。

使用 zardon 的代码,我添加了一个值 << 的语句。 1000 不格式化数字。

这是该方法的代码:

/*
    Override the stringForObjectValue method from NSNumberFormatter

    100 -> 100
    1000 -> 1k
    1 000 000 -> 1m
    1 000 000 000 -> 1b
    1 000 000 000 -> 1t
 */
- (NSString *)stringForObjectValue:(id)anObject {

    // If we don't get a NSNumber, we can't create the string
    if (![anObject isKindOfClass:[NSNumber class]]) {
        return nil;
    }

    NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init];

    // Decimal value from the NSObject
    double doubleValue = [anObject doubleValue];

    NSString *stringValue = nil;

    // Abbrevations used
    NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ;

    // If the value is less than 1000, we display directly the value
    if(doubleValue < 1000.0) {
        stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
    }
    else { // Otherwise we format it as expected
        for (NSString *s in abbrevations) {
            doubleValue /= 1000.0 ;

            if ( doubleValue < 1000.0 ) {

                if ( (long long)doubleValue % (long long) 100 == 0 ) {
                    [nformat setMaximumFractionDigits:0];
                } else {                
                    [nformat setMaximumFractionDigits:2];
                }

                stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
                NSUInteger stringLen = [stringValue length];

                if ( [stringValue hasSuffix:@".00"] )
                {               
                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];            
                } else if ( [stringValue hasSuffix:@".0"] ) {

                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)];

                } else if ( [stringValue hasSuffix:@"0"] ) {

                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];        
                }

                // Add the letter suffix at the end of it
                stringValue = [stringValue stringByAppendingString: s];

                break;
            }   
        }
    }

    [nformat release];

    return stringValue;
}

在接口中我们只需添加继承语句:

@interface MoneyNumberFormatter : NSNumberFormatter

希望这会有所帮助..

Just to improve the solution, a good idea is maybe to subclass the NSNumberFormatter class and override the - (NSString *)stringForObjectValue:(id)anObject method.

Using the code from zardon, I added a statement for the values < 1000 which doesn't format the number.

Here is the code of the method :

/*
    Override the stringForObjectValue method from NSNumberFormatter

    100 -> 100
    1000 -> 1k
    1 000 000 -> 1m
    1 000 000 000 -> 1b
    1 000 000 000 -> 1t
 */
- (NSString *)stringForObjectValue:(id)anObject {

    // If we don't get a NSNumber, we can't create the string
    if (![anObject isKindOfClass:[NSNumber class]]) {
        return nil;
    }

    NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init];

    // Decimal value from the NSObject
    double doubleValue = [anObject doubleValue];

    NSString *stringValue = nil;

    // Abbrevations used
    NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ;

    // If the value is less than 1000, we display directly the value
    if(doubleValue < 1000.0) {
        stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
    }
    else { // Otherwise we format it as expected
        for (NSString *s in abbrevations) {
            doubleValue /= 1000.0 ;

            if ( doubleValue < 1000.0 ) {

                if ( (long long)doubleValue % (long long) 100 == 0 ) {
                    [nformat setMaximumFractionDigits:0];
                } else {                
                    [nformat setMaximumFractionDigits:2];
                }

                stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
                NSUInteger stringLen = [stringValue length];

                if ( [stringValue hasSuffix:@".00"] )
                {               
                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];            
                } else if ( [stringValue hasSuffix:@".0"] ) {

                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)];

                } else if ( [stringValue hasSuffix:@"0"] ) {

                    // Remove suffix
                    stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];        
                }

                // Add the letter suffix at the end of it
                stringValue = [stringValue stringByAppendingString: s];

                break;
            }   
        }
    }

    [nformat release];

    return stringValue;
}

In the interface we simply add the inheritage statement :

@interface MoneyNumberFormatter : NSNumberFormatter

Hope this helps..

血之狂魔 2024-09-20 03:30:32

....好的,感谢 Cocoa Tidbits 博客,我相信我有一个更优雅、更快并且不需要那么多编码的解决方案;它仍然需要测试,并且可能还需要更多的编辑,但它似乎比我原来的要好得多。

我对脚本进行了一些修改,使其不显示任何相关的尾随零;

    NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init];
    [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [nformat setCurrencySymbol:@"$"];
    [nformat setNumberStyle:NSNumberFormatterCurrencyStyle];
    double doubleValue = 10200;
    NSString *stringValue = nil;
    NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ;

    for (NSString *s in abbrevations)
    {

        doubleValue /= 1000.0 ;

        if ( doubleValue < 1000.0 )
        {

            if ( (long long)doubleValue % (long long) 100 == 0 ) {
                [nformat setMaximumFractionDigits:0];
            } else {                
                [nformat setMaximumFractionDigits:2];
            }

            stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
            NSUInteger stringLen = [stringValue length];

            if ( [stringValue hasSuffix:@".00"] )
            {               
                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];            
            } else if ( [stringValue hasSuffix:@".0"] ) {

                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)];

            } else if ( [stringValue hasSuffix:@"0"] ) {

                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];        
            }


            // Add the letter suffix at the end of it
            stringValue = [stringValue stringByAppendingString: s];

            //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]]  , s] ;
            break ;
        }   
    } 

    NSLog(@"Cash = %@", stringValue);

.... Okay, with thanks to the author of the Cocoa Tidbits blog, I believe I have a solution which is much more elegant, faster and doesn't require so much coding; it still needs testing, and it also probably requires a little more editing, but it seems to be much better than my original.

I modified the script a little to make it not show any trailing zeros where relevant;

    NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init];
    [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [nformat setCurrencySymbol:@"$"];
    [nformat setNumberStyle:NSNumberFormatterCurrencyStyle];
    double doubleValue = 10200;
    NSString *stringValue = nil;
    NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ;

    for (NSString *s in abbrevations)
    {

        doubleValue /= 1000.0 ;

        if ( doubleValue < 1000.0 )
        {

            if ( (long long)doubleValue % (long long) 100 == 0 ) {
                [nformat setMaximumFractionDigits:0];
            } else {                
                [nformat setMaximumFractionDigits:2];
            }

            stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ];
            NSUInteger stringLen = [stringValue length];

            if ( [stringValue hasSuffix:@".00"] )
            {               
                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];            
            } else if ( [stringValue hasSuffix:@".0"] ) {

                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)];

            } else if ( [stringValue hasSuffix:@"0"] ) {

                // Remove suffix
                stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];        
            }


            // Add the letter suffix at the end of it
            stringValue = [stringValue stringByAppendingString: s];

            //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]]  , s] ;
            break ;
        }   
    } 

    NSLog(@"Cash = %@", stringValue);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文