Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Casting Integers to Doubles
acodeguy
acodeguy

Posted on

     

Casting Integers to Doubles

This week I got tripped-up by what I reckon is a rookie mistake.

Let's say we're building a clock face with just the hours. We'd use a maximum of 12 to represent each hour. 3 would point to 3 on the clock face, 7 to 7, etc

I was trying to work out how many degrees of a circular gauge view to fill when given twoIntegers representing its current and maximum possible values.

What I did wrong

letcurrent:Int=6// where the hour hand should point to...letmax:Int=12letdegreesToFill:Double=Double(current/max)*360.0
Enter fullscreen modeExit fullscreen mode

How I fixed it:

letdegreesToFill:Double=(Double(current)/Double(max))*360.0
Enter fullscreen modeExit fullscreen mode

The mistake:

It seems that attempting to cast the result of the initial division returns 0 rather than the result as aDouble, as expected.

The way which worked was to cast eachInteger separately, then divide them before multiplying by the maximum number of degrees in the circle.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I make software things for Apple platforms (iOS/MacOS/watchOS)
  • Location
    UK
  • Joined

More fromacodeguy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp