%f 6位 ios

2015-05-19 15:38:51 sqc3375177 阅读数 64449


    self.orderCost.text = [NSStringstringWithFormat:@"%.1f",self.order.cost.floatValue];

%.1f  表示小数点一位,%.2f 表示小数点2位,依次类推.



格式定义
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; the specifiers are summarized in Table 1. Note that you can also use the “n$” positional specifiers such as %1$@ %2$s. For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.

Table 1 Format specifiers supported by the NSString formatting methods and CFString formatting functions
定义 说明
%@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
%% ‘%’ character
%d, %D, %i Signed 32-bit integer (int)
%u, %U Unsigned 32-bit integer (unsigned int)
%hi Signed 16-bit integer (short)
%hu Unsigned 16-bit integer (unsigned short)
%qi Signed 64-bit integer (long long)
%qu Unsigned 64-bit integer (unsigned long long)
%x Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f
%X Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F
%qx Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f
%qX Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F
%o, %O Unsigned 32-bit integer (unsigned int), printed in octal
%f 64-bit floating-point number (double)
%e 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
%E 64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent
%g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%G 64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%c 8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit
%C 16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit
%s Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.
%S Null-terminated array of 16-bit Unicode characters
%p Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x
%L Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument
%a 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent
%A 64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent
%F 64-bit floating-point number (double), printed in decimal notation
%z Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument
%t Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument
%j Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument

平台依赖
Mac OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 2. Note that in some cases you may have to cast the value.

Table 2 Format specifiers for data types
类型 定义 建议
NSInteger %ld or %lx Cast the value to long
NSUInteger %lu or %lx Cast the value to unsigned long
CGFloat %f or %g %f works for floats and doubles when formatting; but see below warning when scanning
CFIndex %ld or %lx The same as NSInteger
pointer %p %p adds 0x to the beginning of the output. If you don’t want that, use %lx and cast to long.
long long %lld or %llx long long is 64-bit on both 32- and 64-bit platforms
unsigned long long %llu or %llx unsigned long long is 64-bit on both 32- and 64-bit platforms

The following example illustrates the use of %ld to format an NSInteger and the use of a cast.

1
2
NSInteger i = 42;
printf("%ld\n"(long)i);

In addition to the considerations mentioned in Table 2, there is one extra case with scanning: you must distinguish the types for float and double. You should use %f for float, %lf for double. If you need to use scanf (or a variant thereof) with CGFloat, switch to double instead, and copy the double to CGFloat.

1
2
3
4
CGFloat imageWidth;
double tmp;
sscanf (str, "%lf"&tmp);
imageWidth = tmp;

It is important to remember that %lf does not represent CGFloat correctly on either 32- or 64-bit platforms. This is unlike %ld, which works for long in all cases.


2016-03-13 23:16:59 u012382791 阅读数 1892

 

%d,%i 以带符号的十进制形式输出整数(证书不输出符号)
%o 以八进制无符号形式输出整数(不输出0前缀)
%x 以十六进制无符号形式输出整数(不输出0x前缀)
%u 以无符号十进制形式输出整数
%c 以字符形式输出,只输出一个字符
%s 输出C风格的字符串
%f 以小数形式输出浮点数,默认输出6位小数
%e 以指数形式输出浮点数,数字部分默认输出6位小数
%g 自动选用%f或%e其中之一,保证输出宽度较短的格式,并且不会输出无意义的0
%p 以十六进制形式输出指针变量所代表的地址值
%@ 输出Objective-C对象

在%与格式字符之间,还可插入以下附加符号:

l(字母) 可在格式字符d、o、x、u之前,用于输出长整型整数;也可在f、e、g之前,用于输出长浮点型数
m(代表一个正整数) 指定输出数据所占的最小宽度
.n 对于浮点数,表示输出n位小数;对于字符串,表示截取的字符个数
- 表示输出的数值向左边对齐

 

2018-08-02 14:17:26 bit_zsk 阅读数 2138

参考:

https://ephrain.net/cordova-%E5%A2%9E%E5%8A%A0-ios-platform-%E6%99%82%EF%BC%8C%E5%87%BA%E7%8F%BE-failed-to-fetch-platform-cordova-ios-%E7%9A%84%E5%95%8F%E9%A1%8C/

 

 

 

今天想用 Cordova 編譯出給 iOS 用的 app,

用 cordova platform add ios 指令,結果遇到了錯誤:

testuser@localhost ~ $ cordova platform add ios

Using cordova-fetch for cordova-ios@^4.4.0
Error: Failed to fetch platform cordova-ios@^4.4.0
Probably this is either a connection problem, or platform spec is incorrect.
Check your connection and platform name/version/URL.
Error: npm: Command failed with exit code 254 Error output:
npm ERR! Darwin 16.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "cordova-ios@^4.4.0" "--save"
npm ERR! node v6.2.1
npm ERR! npm  v3.9.3
npm ERR! path /Users/testuser/node_modules/.staging/xml-escape-d3143b78
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename

npm ERR! enoent ENOENT: no such file or directory, rename '/Users/testuser/node_modules/.staging/xml-escape-d3143b78' -> '/Users/testuser/node_modules/cordova-ios/node_modules/xml-escape'
npm ERR! enoent ENOENT: no such file or directory, rename '/Users/testuser/node_modules/.staging/xml-escape-d3143b78' -> '/Users/testuser/node_modules/cordova-ios/node_modules/xml-escape'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! Please include the following file with any support request:

 

嗯… 看起來很詭異的錯誤…

我不知道這個 node_modules/.staging 是什麼東西,

試著用 npm 裝了 xml-escape 這個模組無效,

把裝好的 xml-escape 從 node_modules/xml-escape,

移到 node_modules/.staging 目錄下也無效…

 

上網查了一下,又試了以下幾個方法,全都無效:

1. 清掉 npm 的快取:

npm cache clean --force

 

2. 重裝 Cordova:

npm uninstall -g cordova
npm install -g cordova

 

3. 移除 ~/.cordova 目錄

rm -rf ~/.cordova

 

4. 升級 npm

npm update -g npm

 

後來看到有人說它升級了 node.js…

死馬當活馬醫,去 node.js 網站上抓了 Mac 版最新的 node.js 來安裝,

原本:node: 6.2.1, npm: 3.9.3

後來:node: 6.10.3, npm: 3.10.10

 

這邊比較怪的是,之前我用 npm update -g npm,

最多也只升到 3.9.10,不知道為啥沒辦法升到附在 node.js 裡的 npm 3.10.10?

不過,升完 node.js 之後,問題消失了,

不確定是升級 node.js 還是 npm 的效果:

testuser@localhost ~ $ cordova platform add ios

Using cordova-fetch for cordova-ios@^4.4.0
Adding ios project...
Creating Cordova project for the iOS platform:
    Path: platforms/ios
    Package: idv.ephrain.multilibrarylogin
    Name: Library Multi-Login
iOS project created with cordova-ios@4.4.0
Installing "cordova-plugin-admobpro" for ios
Plugin dependency "cordova-plugin-extension@1.5.1" already fetched, using that version.
Installing "cordova-plugin-extension" for ios
Installing "cordova-plugin-dialogs" for ios
Dependent plugin "cordova-plugin-extension" already installed on ios.
Installing "cordova-plugin-inappbrowser" for ios
Installing "cordova-plugin-statusbar" for ios
Installing "cordova-plugin-whitelist" for ios
--save flag or autosave detected
Saving ios@~4.4.0 into config.xml file ...

 

總之,有遇到問題的人,可以每個方法都嘗試看看… Orz

2016-12-08 09:10:19 sg_zxw 阅读数 3869

最近做项目时遇到了一个很有意思的问题,直接看图吧!


看看神奇吧,当时我就懵逼了,难道是我的数学没学好???

根据小学时学的数学知识,老师告诉我们四舍五入的原则是:看下一位的数字如果小于或者等于4那么就舍去,如果大于等于5那就进一位,那么根据这个原则,0.125保留两位小数就是0.16,然而在ios里面四舍五入的原则并非这样,看图就明白了,安卓里面也是这样(我让安卓的哥们测试了下得到了同样的结果),对于这个问题,大家怎么看?

2013-11-23 10:58:04 u011872945 阅读数 3468

1、IOS 格式化输出符号

 %@    对象

%d,%i 整数

%u   无符号整形

%f    浮点(双字节)

%x,%X  二进制整数

%o  八进制整数

%zi    正整数

%zu   size_t

%p  指针

%e  浮点/双字节(科学计算)

%g  f浮点/双字节

%s   C 字符串

%。*s  pascal字符串

%c   字符

%C unichar

%lld  64位长整数

%llu  无符号64位长整数

%Lf   64位双字节

2、数据类型转换

 2.1、NSString转换为int

    NSString *intstring=@"123";

    int i=[intstring integerValue];

2.2、int转换字符串

   

     NSString *str=[NSString stringWithFormat:@"%d",i];

2.3、字符转换为float

     

     float f=[str floatValue];

2.4、float转换为字符串

     

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



IOS NSLog 输出语法

阅读数 2337

IOS6新特性

阅读数 1776

ios时间戳13位转换

阅读数 152122