如何将字符串类 “10_000_00” 转换为整数类 10_000_00。

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

How to convert "10_000_00" string class to 10_000_00 Integer class

问题

如何将以下“整数”字符串转换为它们下划线分隔的Integer类对应项?

示例:

"10_000_00" => 10_000_00

"500_000_00" => 500_000_00

等等。

英文:

How can I convert the following "integer" strings into there underscored Integer class counter parts?

Examples:

"10_000_00" => 10_000_00

"500_000_00" => 500_000_00

etc. etc

答案1

得分: 3

String#to_i 将数字的字符串表示转换为整数:

"10_000_00".to_i
# => 1000000

"500_000_00".to_i
# => 50000000

请注意,整数中的下划线仅仅是在源代码中为了更易读而添加的语法糖,但这些整数并没有特殊之处:

10_000_00 == 1000000
# => true
英文:

String#to_i translates the string representation of a number to an integer:

"10_000_00".to_i
#=> 1000000

"500_000_00".to_i
#=> 50000000

Note that underscores in integers are only syntactic sugar to make the more readable in source code, but there is nothing special about those integers

10_000_00 == 1000000 
#=> true

huangapple
  • 本文由 发表于 2023年7月20日 14:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727121.html
匿名

发表评论

匿名网友

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

确定