我已經得到了這個基本的腳本,我需要轉換爲目標c,它將大量單位的資金轉換爲縮短版本(即:1.2m等)已經完成了大部分轉換,但我遇到的最大問題是最後。將基本腳本轉換爲Objective C(貨幣格式)
原來的基本代碼是:
; 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是最後一個數字。
有人可以仔細檢查一下,或者有更簡單的方法可以做到這一點。無論如何,感謝您的幫助。
@Fabien我這個去解決方案,但感謝您的幫助無論如何 – zardon 2011-05-30 20:22:04