如何使用Swift将整数数组值转换为双精度数。

huangapple go评论81阅读模式
英文:

How to convert Int of Array values to Double using Swift

问题

在我的场景中,我试图在整数值数组之前添加零点。例如,我的整数值数组如下:50,但在传递给AMProgress之前,我需要添加零,使其变为0.50。如果出现0.100,需要更改为1。如何实现这一目标?

我的代码如下

let data: [Int] = [1, 2, 4, 5, 6, 7, 8, 4, 23, 54, 100]
所需输出:0.10.20.3、等等...
英文:

In myscenario, I am trying to add zero with point before array of Integer value. For example my array of integer values like 50 but before passing to AMProgress I need to add like 0.50. If supposed 0.100 came need to change like 1. How to achieve this?

My Code Below

let data : [Int] = [1,2,4,5,6,7,8,4,23,54,100]    
Required output: 0.1, 0.2, 0.3, etc,...

答案1

得分: 1

Output:

[0.01, 0.02, 0.04, 0.05, 0.06, 0.07, 0.08, 0.04, 0.23, 0.54, 1.0]

英文:
    let data : [Int] = [1,2,4,5,6,7,8,4,23,54,100]
    
    let convertedData = data.map { Double($0)/100}

Output:

[0.01, 0.02, 0.04, 0.05, 0.06, 0.07, 0.08, 0.04, 0.23, 0.54, 1.0]

huangapple
  • 本文由 发表于 2020年1月3日 18:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定