object-c中NSString与int和float NSDictionary NSString json的相互转换

来源:互联网 发布:什么决定命运知乎 编辑:程序博客网 时间:2024/06/08 01:30

NSString *tempA = @"123";

NSString *tempB = @"456";

 

1,字符串拼接

 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

 

2,字符转int

int intString = [newString intValue];

 

3,int转字符

NSString *stringInt = [NSString stringWithFormat:@"%d",intString];

 

4,字符转float

 float floatString = [newString floatValue];

 

5,float转字符

NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];

6,double 转NSString

NSNumber *number = [NSNumber numberWithDouble:345.2343434343];
NSString *aString = [number stringValue];

7,NSDictionary 转 NSString

-(NSString*)DataTOjsonString:(id)object{    NSString *jsonString = nil;    NSError *error;    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string                                                         error:&error];    if (! jsonData) {        NSLog(@"Got an error: %@", error);    } else {        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];    }    return jsonString;}
8,拼接Json字符串

NSDictionary *dataDictionary= [NSDictionary dictionaryWithObjectsAndKeys:@"mac",@"mac",
@"game",@"game",
@"devicetoken",@"devicetoken",
@"device",@"device",
@"gv",@"gv",
@"lang",@"lang",
@"os",@"os",nil];
NSDictionary *parmDictionary= [NSDictionary dictionaryWithObjectsAndKeys:@"getSession",@"act",
dataDictionary,@"data",nil];
NSDictionary *jsonDictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"pv",@"pv",
parmDictionary,@"param",nil];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];

NSString *jasonString = [writer stringWithObject:jsonDictionary];
NSLog(@"%@",jasonString);
上面的代码用到了三层嵌套,注意的是,NSDictionary中,前面的是值,后面的是键。
拼接后的结果如下:

{"pv":"pv","param":{"act":"getSession","data":{"os":"os","mac":"mac","game":"game","gv":"gv","lang":"lang","devicetoken":"devicetoken","device":"device"}}}
使用 Json在线校验工具 解析后:
{
"pv": "pv",
"param": {
"act": "getSession",
"data": {
"os": "os",
"mac": "mac",
"game": "game",
"gv": "gv",
"lang": "lang",
"devicetoken": "devicetoken",
"device": "device"
}
}
}

四舍五入问题

-(NSString *)notRounding:(float)price afterPoint:(int)position{

    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

    NSDecimalNumber *ouncesDecimal;

    NSDecimalNumber *roundedOunces;

    

    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];

    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

    [ouncesDecimal release];

    return [NSString stringWithFormat:@"%@",roundedOunces];

}

介绍一下参数:

price:需要处理的数字,

position:保留小数点第几位,

然后调用

 

    float s =0.126;

    NSString *sb = [self notRounding:s afterPoint:2];

    NSLog(@"sb = %@",sb);

输出结果为:sb = 0.12

 

接下来介绍NSDecimalNumberHandler初始化时的关键参数:decimalNumberHandlerWithRoundingMode:NSRoundDown,

NSRoundDown代表的就是 只舍不入。

scale的参数position代表保留小数点后几位。

0 0
原创粉丝点击