【问题标题】:how to show only two digits after the decimal in float value in iphone如何在iphone中的浮点值中仅显示小数点后两位
【发布时间】:2012-05-22 22:19:07
【问题描述】:

我有一个浮点数,我希望它只显示小数点后的两位数字,就像它是 1.566666 一样,它只显示 1.56

我正在使用这种方式在我的代码中浮动

    float ProfitForCerenia=costtoclient/markup;

float totalProfit=costtoclient-ProfitForCerenia;

【问题讨论】:

  • 你可能不应该用花车来赚钱。一般来说,使用整数是个好主意,因为在加法/减法时不会出现任何舍入错误。

标签: objective-c xcode


【解决方案1】:

我假设你想要一个带有你的号码的 NSString:

[NSString stringWithFormat:@"%.02f", floatVal];

【讨论】:

  • 不,我的浮点数现在是 1.555555,我希望它只是 1.55
  • 所以你想改变你的浮动? floatVal = ((int)(floatVal * 100)) / 100;但这真的很难看……
猜你喜欢
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 2018-02-12
  • 2013-02-23
  • 2020-06-29
  • 2011-09-03
  • 2011-01-18
  • 2017-07-04
相关资源
最近更新 更多