将小数作为整数

时间:2017-11-08 21:10:56

标签: python numpy

我需要取一个像(0.25)这样的数字并反复乘以10,直到小数点后面没有值。

例如,如果数字是.25,那么我需要反复乘以10:

.25 * 10 * 10 = 25

或者号码是1.5

1.5 * 10 = 15

最好的方法是什么?一个for循环? 我知道我应该做点什么

number = .75
for i in range(0,10):
    number * 10**i = ???? (this is where I am stuck)

3 个答案:

答案 0 :(得分:2)

你可以这样做

x=.25
while int(x) != x:
    x=x*10

答案 1 :(得分:0)

如果输入总是正分数(1> x> 0),我会试试这个:

lazy var circle =
{
   let target = SKShapeNode(circleOfRadius: 1000)
   target.fillColor = .white
   //target.strokeColor = .black //if stroke is anything other than black, you may need to do 2 SKSpriteNodes that layer each other
   target.lineWidth = 8 * 1000 / 35

   let texture = SKView().texture(from:target)
   let spr = SKSpriteNode(texture:texture)
   spr.physicsBody = SKPhysicsBody(circleOfRadius: 1000)
   addStandardProperties(node: spr, name: "circle", z: 5, contactTest:ContactCategory, category: CircleCategory) //Sets physicsBody properties
   return spr
}()

func createCircle(of radius:CGFloat,color:UIColor) -> SKSpriteNode
{
   let spr = circle.copy()
   let scale = radius/1000.0
   spr.xScale = scale
   spr.yScale = scale
   spr.color = color
   spr.colorBlendFactor = 1.0
   return spr

}

func addCircle() {

    let attributes = getTargetAttributes() //sets size, position, and color of the circle
    let spr = createCircle(of:attribute.width,color:attributes.color)
    spr.position = attributes.position
    addChild(str)
}

答案 2 :(得分:0)

另一种方式

x=.25
while x % 1.0 != 0:
    x=x*10