将英寸转换为厘米,反之亦然 [英] Transform inches to centimeters and viceversa

查看:190
本文介绍了将英寸转换为厘米,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对objective-c非常陌生。



我试图创建一个需要英寸或厘米的函数,并将这些值转换为相反的度量系统。 / p>

我目前正在努力研究如何在objective-c中做一个数学方程,因为我不知道如何设置变量。因为我总是得到一些错误从XCode说,变量是一个int,但不应该或指针是错误和其他东西... basicaly ...我不知道如何使数学...



我的意思是我传递字符串 5'11 1.80 cm



下面的代码试图通过英寸到厘米....如果我们通过5'5数学方程应为((5 * 12)+ 3)* 2.54 这等于 180.34 结果,我从功能看是 1.80厘米



下面的代码是一个我得到的,但它只是不为我工作。

   - (NSString *)returnRightMetric:如果任何人都可以告诉我如何在我的代码中执行此操作。 (NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType 
{
if([metricType isEqualToString:@inches]){

NSArray * theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@']];
NSInteger * value1 = [theConvertion [0] intValue];
NSInteger * value2 = [theConvertion [1] intValue];

float * number =((value1 * 12)+ value2)* 2.54;
/// .....

} else if([metricType isEqualToString:@cm])
{
}

return result;
}


解决方案

  NSArray * theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@']];
int value1 = [theConvertion [0] intValue];
int value2 = [theConvertion [1] intValue];

float number =((value1 * 12)+ value2)* 2.54;
NSString * formattedNumber = [NSString stringWithFormat:@%。02f,(number / 100)];

简而言之,你需要知道native / primitive类型和引用类型之间的区别指向此字符 - > *。这可能有助于链接


I am quite new to objective-c.

I am trying to create a function that takes inches or centimetres and transform the values into the oposite metric system.

I am currently struggling on how to do a mathematical equation in objective-c given that I do not know how to set the variables accordingly. As I always get some error from XCode saying that the variable is an int but shouldn't be or the pointer is wrong and other things... basicaly... i dont know how to make math...

The intention is that I pass the string 5’11" or 1.80 cm as stored somewhere else and convert it to the opposite metric system.

The code below attempts to do pass inches to cm.... if we pass 5’5" the mathematical equation should be ((5 * 12) + 3) * 2.54 this is equal to 180.34 and the formated result that i look from the function is 1.80 cm

The following code is a far as I got but it just not working for me. if anyone could please tell me how to do this operation in my code I would appreciate it.

- (NSString *)returnRightMetric:(NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType
{
    if ([metricType isEqualToString:@"inches"]) {

    NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
                                [NSCharacterSet characterSetWithCharactersInString:@"’""]];
    NSInteger* value1 = [theConvertion[0] intValue];
    NSInteger* value2 = [theConvertion[1] intValue];

    float *number = ((value1 * 12) + value2) * 2.54;
///.....

    } else if ([metricType isEqualToString:@"cm"])
    {
    }

    return result;
}

解决方案

NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
                          [NSCharacterSet characterSetWithCharactersInString:@"’""]];
int value1 = [theConvertion[0] intValue];
int value2 = [theConvertion[1] intValue];

float number = ((value1 * 12) + value2) * 2.54;
NSString *formattedNumber = [NSString stringWithFormat:@"%.02f", (number/100)];

So in short you need to know the difference between native/primitive type's and reference types (type that use pointers this character --> *. This might help Link

这篇关于将英寸转换为厘米,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆