What does the underscore mean in literal numbers?

后端 未结 4 514
孤独总比滥情好
孤独总比滥情好 2020-12-29 01:22

What does that mean?
0.0..10_000.0

相关标签:
4条回答
  • 2020-12-29 01:35

    It’s just a syntax convenience to separate the thousands:

    $ ruby -e 'puts 1_000 + 1_000_000'  #=> 1001000
    
    0 讨论(0)
  • 2020-12-29 01:53

    Underscores are ignored. You can put them in to make them more readable.

    0 讨论(0)
  • 2020-12-29 01:54

    Actually all other answers here are wrong.

    _ is not ignored, just try it with 0_50:

    > 1_50
     => 150
    > 0_50
     => 40
    

    YEAAAAAAH YOU WILL FREAK OUT IF YOU JUST WANTED TO USE IT FOR DECIMALS :(

    In general it just describes a range of numbers, like CCD mentions above.

    As Kyle Heironimus commented, the underscore is actually ignored.

    0 讨论(0)
  • 2020-12-29 01:56

    It is a Range object, of the kind a..b

    In this case it gives you the numbers from 0 to 10,000 as Floats.

    the underscore '_' is ignored, and used for readability, so 10_000 is equivalent 10,000.

    Buy adding .0 to each part of the range, the numbers would be considered as floats instead of integers, so you won't be able to iterate over the range (the each method would raise an exception).

    0 讨论(0)
提交回复
热议问题