У меня есть этот базовый скрипт, который мне нужно преобразовать в цель c, он превращает большие денежные единицы в укороченные версии (т. Е. 1,2 м и т. Д.), У меня большая часть конвертации выполнена, но Самая большая проблема у меня в самом конце.
Оригинальный базовый код:
; 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
// Следующее сообщение отредактировано Thur 5 Aug автором сообщения.
Я полагаю, что теперь у меня все отсортировано, у меня на данный момент работают следующие тысячи, я не уверен, будет ли это работать при любых обстоятельствах, и буду рад любой помощи / руководству по этому вопросу. Я знаю о проблемах с памятью, но позже я разберусь с ними, это часть, касающаяся манипуляции со строками, которую я сначала решаю.
// 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 - последнее число.
Может кто-то просто дважды проверить это, или, возможно, есть более простой способ сделать все это. В любом случае, спасибо за вашу помощь.