[英]NSNumber and decimal value
I have an NSNumber like this for example = 1978, i would like to convert this for : 1K9, seconde example : 35700 convert to : 35K7 ( where "k" is kilometers and "M" is meters, how i can do this 我有一个像这样的NSNumber例如1978,我想将其转换为:1K9,第二个例子:35700转换为:35K7(其中“ k”是公里,“ M”是米,我该怎么做
thanks 谢谢
int temp;
NSNumber *yourNumber;//the number you enter from some where
NSString *newValue;
if([yourNumber intValue]>1000){
temp = [yourNumber intValue] % 1000 ;//your number module 1000
newValue= [[temp stringValue]stringByAppendingString:@"K"];
}
Note: I haven't my mac with me, if the [temp stringValue]
gives any worning&error please inform me. 注意:我还没有Mac,如果
[temp stringValue]
任何磨损和错误,请通知我。
Here's how: 这是如何做:
NSNumber *initialNumber = [NSNumber numberWithInt:35700];
NSString *resultString = [NSString stringWithFormat:@"%iK%i", floor(initialNumber / 1000), floor((initialNumber % 1000) / 100)];
Basically you can work with the internal number data. 基本上,您可以使用内部号码数据。 Assuming you are working on a meter-based value, you might want something like this:
假设您正在使用基于计量器的值,则可能需要以下内容:
NSNumber *sourceValue = ... // your NSNumber value from any source
int meters = sourceValue.intValue;
int km = floor(meters / 1000); // only your kilometers
int sub_km = meters % 1000; // only the part behind the kilometers
int first_sub_km = floor(sum_km / 100); // the first digit of the subrange
NSString *readable = [NSString stringWithFormat:@"%iK%i", km, first_sub_km];
First, you split the meters into <= 1000 and > 1000. Then you'll just have to put that out formatted, with a K in between. 首先,将米划分为<= 1000和>1000。然后,只需将其格式化,中间插入一个K。
Write your own subclass of NSNumberFormatter. 编写自己的NSNumberFormatter子类。 In this subclass you can implement the calculation logic.
在此子类中,您可以实现计算逻辑。
The logic might look like this. 逻辑可能看起来像这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.