ios NSString format 保留小数点 float double

摘要:
自己订单成本。text=[NSStringStringWithFormat:@“%.1f元”,self.order.cocost.floatValue];%1f表示小数点后一位,%。2f表示两位小数,依此类推。格式定义NSString格式化方法和CFStringformating函数支持的格式说明符

 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, %iSigned 32-bit integer (int)
%u, %UUnsigned 32-bit integer (unsigned int)
%hiSigned 16-bit integer (short)
%huUnsigned 16-bit integer (unsigned short)
%qiSigned 64-bit integer (long long)
%quUnsigned 64-bit integer (unsigned long long)
%xUnsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f
%XUnsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F
%qxUnsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f
%qXUnsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F
%o, %OUnsigned 32-bit integer (unsigned int), printed in octal
%f64-bit floating-point number (double)
%e64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
%E64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent
%g64-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
%G64-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
%c8-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
%C16-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
%sNull-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.
%SNull-terminated array of 16-bit Unicode characters
%pVoid pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x
%LLength modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument
%a64-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
%A64-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
%F64-bit floating-point number (double), printed in decimal notation
%zLength 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
%tLength 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
%jLength 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 %lxCast the value to long
NSUInteger%lu or %lxCast 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 %lxThe 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 %llxlong long is 64-bit on both 32- and 64-bit platforms
unsigned long long%llu or %llxunsigned 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 ", (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.

免责声明:文章转载自《ios NSString format 保留小数点 float double》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Visual Studio 解决方案版本从v12-->v14雷电USB4开源示波器,4通道,带宽350MHz,采样率1Gsps,上位机支持Windows和Linux下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

poi excel导入整数数字自动加小数点解决

private String numberFormat(Cell cell){ NumberFormat numberFormat = NumberFormat.getInstance(); // 不显示千位分割符,否则显示结果会变成类似1,234,567,890 numberFormat.setGroupingUsed(false); i...

控制input标签中只能输入数字以及小数点后两位

js 代码如下: /*控制input标签中只能输入数字 和小数点后两位 */ functioncheckNum(obj) { //检查是否是非数字值 if(isNaN(obj.value)) { obj.value = ""; } if (obj != null) { //检查小数点后是否对于两位http://blog.csdn.net/sha...

java中int相除取小数点后两位或限定位数

java两个整数相除保留两位小数: http://blog.sina.com.cn/s/blog_624d755d0101cvuq.html java中,当两个整数相除时,由于小数点以后的数字会被截断,运算结果将为整数,此时若希望得到运算结果为浮点数,必须将两整数其一或是两者都强制转换为浮点数。例如:(float)a/b //将整数其中一个强制转换为浮点数...

mysql cast函数

CAST函数语法规则是:Cast(字段名 as 转换的类型 ),其中类型可以为: CHAR[(N)] 字符型 DATE 日期型DATETIME 日期和时间型DECIMAL float型SIGNED intTIME 时间型 实例1: 表table1 date:2015-11-03 15:31:26 select cast(date as signed)...

[Oracle] Oracle和SQLServer的数据类型比较

类型名称 Oracle SQLServer 比较 字符数据类型 CHAR CHAR 都是固定长度字符资料但oracle里面最大度为2kb,SQLServer里面最大长度为8kb 变长字符数据类型 VARCHAR2 VARCHAR Oracle里面最大长度为4kb,SQLServer里面最大长度为8kb 根据字符集而定的固定长度字符串 NCH...

linq的decimal类型保存到数据库只保存到小数点后两位的问题

今天的一个decimal类型保存到数据的问题困扰了我很长时间,最后就是一个小小的设置问题解决······坑······深坑···· 话不多说,直接说问题,在说答案: 问题:linq当采用EF的DbContext保存decimal类型数据到数据库,默认只会保存小数点后的前2位小数,其余均置0;         例如保存1.23456789,实际存到数据库里的...