search

Home  >  Q&A  >  body text

ios - float 保留两位小数

比方说3.1415926,我现在想传给后台的是3.14,类型是必须是float类型,该怎么样做?
还有即便通过运算得到了3.14,然后我要是按float传过去,是不是本质上传的还是3.1400000?

伊谢尔伦伊谢尔伦2807 days ago1283

reply all(6)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:59:04

    It should be possible to use string formatting, because what is received in the background is also a string type, not a float type, just use .2f directly

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:59:04

    Why are all strings transmitted from the front to the backend? Float doesn’t have any number of decimal places, or bigdecimal

    reply
    0
  • PHPz

    PHPz2017-04-18 09:59:04

    The double type is more accurate. You can also try the NSDecimalNumber class

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:59:04

    Use floor 函数即可:
    double re = floor(numFloat*100) / 100;

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:59:04

    You can use toFixed(2) to retain two decimal places

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:59:04

    .2f is generally acceptable

    reply
    0
  • Cancelreply