%lf double ios_double 输出printf("%.lf\n")全出0 - CSDN
精华内容
参与话题
  • [NSString stringWithFormat: @"%.2lf", total] 效果:
    展开全文
  • self.orderCost.text = [NSString stringWithFormat:@"%.1f元",self.order.cost.floatValue]; %.1f 表示小数点一位,%.2f 表示小数点2位,依次类推. ...The format specifiers supported by the NSString ...


        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.


    展开全文
  • 近日,项目中出现了后台返回数据,特别是价格时,小数位会出现很多9999的现象,而在谷歌浏览器测试后台返回数据时,正常显示是只有2位,后台也说只返给了2位小数给我,百思不得其解,只能认为是开发环境不同造成的...

    近日,项目中出现了后台返回数据,特别是价格时,小数位会出现很多9999的现象,而在谷歌浏览器测试后台返回数据时,正常显示是只有2位,后台也说只返给了2位小数给我,百思不得其解,只能认为是开发环境不同造成的问题。之前的解决方案是利用 %.2f 进行截取四舍五入,其实可以正常使用,只是保留了两位小数。

    现在发现其实是,NSNumber的description方法不够严谨,在调用NSNumber的description方法打印数值时,会发生精度损失。如果是double类型,建议把NSNumber转换成double再进行输出或计算。


    用json格式化工具或谷歌浏览器json解析插件显示的正常的:


    如何解决:

    在后台返回float或者double类型时,利用NSDecimalNumber处理精度丢失问题。此处我是以string方式接收的,直接添加NSString的分类,修正方法贴出来如下:

    /*!
     @brief 修正浮点型精度丢失
     @param str 传入接口取到的数据
     @return 修正精度后的数据
     */
    +(NSString *)reviseString:(NSString *)str
    {
        //直接传入精度丢失有问题的Double类型
        double conversionValue = [str doubleValue];
        NSString *doubleString = [NSString stringWithFormat:@"%lf", conversionValue];
        NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:doubleString];
        return [decNumber stringValue];
    }

    我遇到的并不是所有解析出的小数都是精度丢失的,同一个接口内有部分数据如此,且安卓并未出现,所以这边在前端可以自己处理下。

    或者让后台返回时返字符串类型的数据,不返NSNumber类型。



    展开全文
  • iOS double类型精度问题以及计算问题

    千次阅读 2015-01-23 20:13:41
    但是利用double类型数据会不稳定. 1:控制double类型的精度  double abc = 0.1 - 0.01 - 0.00001009;  NSNumberFormatter *nf = [[NSNumberFormatteralloc ]init];  [nf setMaximumIntegerDigits:8];  NSNu

       

       当设计到金钱的计算.但是利用double类型数据会不稳定.

    1:控制double类型的精度

        double abc = 0.1 - 0.01 - 0.00001009;

        NSNumberFormatter *nf = [[NSNumberFormatteralloc ]init];

        [nf setMaximumIntegerDigits:8];

        NSNumber *number = [nfnumberFromString:[NSString stringWithFormat:@"%.8lf",abc]];

        NSLog(@"number is ---%lf",[numberdoubleValue]);


    2:对于double类型不损失精度的计算方法

    -(CGFloat)addreeBackMoneyWithAmount:(CGFloat)amount ToMoney:(CGFloat)toMoney

    {

        NSString *amountStr = [NSString stringWithFormat:@"%.08lf",amount];

        NSString *toMoneyStr = [NSString stringWithFormat:@"%.08lf",toMoney];

        

        NSDecimalNumber *amountNum = [NSDecimalNumber decimalNumberWithString:amountStr];

        NSDecimalNumber *toMoneyNum = [NSDecimalNumber decimalNumberWithString:toMoneyStr];

        double xiaofee = 0.001210000;

        NSDecimalNumber *feeNum = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.8lf",xiaofee]];

        NSDecimalNumber *resultNum = [amountNum decimalNumberBySubtracting:toMoneyNum];

        NSDecimalNumber *subTracFeeNum = [resultNum decimalNumberBySubtracting:feeNum];

        

        return [subTracFeeNum doubleValue];;

    }

    展开全文
  • 前几天在IOS开发时发现一个JSON解析奇葩问题,会出现一定概率的错误,如下://出现BUG的条件必须是两位数,且带两位小数,类型还必须是float //两位数:十位必须是7、8、9;个位数随意 //两位小数:个位数随意;十...
  • ARLineChart是一个iOS的折线图,使用Objective-C编写。适用于需要有双Y轴的应用场景。 x轴和y轴可以放大和缩小,可以滚动,双击可以还原原始大小。
  • iOS—NSLog各种打印格式

    千次阅读 2015-09-21 15:49:42
    NSLog各种打印格式: %@ 对象 ...%f 浮点型和double型 %0.2f 精度浮点数,只保留两位小数 %x: 为32位的无符号整型数(unsigned int),打印使用数字0-9的十六进制,小写a-f; %X: 为32位的无符号整型
  • iOS 解决相关的精度问题

    千次阅读 2016-07-29 11:13:40
    最近在项目中遇到一些问题;虽说这个问题不难,但是...通过网络请求 直接得到一些数据Data ,通过iOS 系统自带的解析方式: NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:N
  • C/C++读写文本文件、二进制文件

    万次阅读 多人点赞 2017-12-13 11:11:17
    C语言文本文件读写方式;C语言二进制文件读写方式;CPP文本文件读写方式;CPP二进制文件读写方式;
  • C++的cout输出小数的精度控制

    千次阅读 2018-06-28 15:17:44
    c++ cout 输出小数的精度cout.flags(ios::fixed);cout.precision(4); //设置输出精度printf("%.4f\n",XXX); 精度输出float : 单精度浮点数double : 双精度浮点数两者的主要区别如下: 01.在内存中占有的...
  • 要求传入的数据类型为long double,那么输出这种类型的数据要用%Lf的方式 要求传入的数据类型为double,那么输出这种类型的数据要用%f的方式 要求传入的数据类型为float,那么输出这种类型的数据要用lf的方式 当...
  • IOS开发笔记7-C语言基础复习

    万次阅读 2016-12-06 08:12:28
    转载请标明出处: ... 本文出自:【顾林海的博客】 前言所谓的通过引用传递,就是在调用函数时传入某个地址(引用),然后由函数将数据存入该地址指向的...实例#include "stdio.h"void circleArea(double radius,double *a
  • 距离计算方法 ios开发

    2014-12-22 09:06:16
    // NSLog(@"%lf//%lf//%lf//%lf",begin.x,begin.y,end.x,end.y); double R = 6371.0; double distance = 0.0; double dLat = (begin.x - end.x) * M_PI / 180.0; double dLon = (begin.y - end.y) * M_PI / 180...
  • IOS计算器的实现

    千次阅读 2015-07-20 08:18:10
    /* 计算器的简单实现 1.加法 2.减法 3.乘法 4.除法 实现两个数的运算 */
  • C++格式化输出小数点后位数

    千次阅读 2017-07-24 02:07:56
    C++格式化输出小数点位数C语言中可以用 printf(“%.2lf\n”, num); 输出指定位数的浮点数,那么C++输出指定位数浮点数的方法是:#include using std::ios; using std::cin; using std::cout; using std::endl;int ...
  • IOS开发教程--NSLog输出格式控制

    千次阅读 2015-08-19 10:20:14
    %@ Object %d, %i signed int %u unsigned int %f float/double %x, %X hexadecimal int %o octal int %zu size_t ...%e float/double (in scientific notation) ...%g float/double (as %f or %e, de
  • 这两天在vs2005环境下做个项目(至于什么项目在此不便多说),碰到了怎么去掉double数据后面的零0及取得double的小数部分的有效 位的小问题(因为项目中的有个功能的需要)。问题虽小,但我想还是有必要在此说两句,...
  • 当调用带可变长度参数列表的函数时,编译器会安排float参数自动转换成为double类型,其结果是printf函数无法区分float型和double型的参数。因此在printf函数调用中%f既可以表示float型又表示double型的参数。  
  • iOS小数去除末位无效零问题

    千次阅读 2017-02-16 23:07:52
    iOS小数去除末位无效零问题
  • 先贴出错误算法: #include int main() { double a = 3.155; printf("%lf\n", a); a = (int)((a * 100) + 0.5) / 100.0; printf("%lf", a); return 0; }测试 : 据说此方
1 2 3 4 5 ... 20
收藏数 4,980
精华内容 1,992
关键字:

%lf double ios