Ruby编程之移除换行符和数字格式化
【摘要】 笔者主要介绍了Ruby编程时去掉换行符和规范数字打印格式
# NOTE: s1 includes a carriage return and linefeed
s1 = "Hello world
"
s2 = "Hello world"
puts(s1.chop) # returns "Hello world"
puts(s1.chomp) # returns "Hello world"
puts(s2.chop) # returns "Hello worl" – note the missing 'd'!
puts(s2.chomp)# returns "Hello world"
Hello world
Hello world
Hello worl
Hello world
puts(s2.chomp('rld')) # returns "Hello wo"
Hello wo
printf( "%f\n", 10.12945 ) #=> 10.129450
printf( "%0.02f\n", 10.12945 ) #=> 10.13
printf("d=%d f=%f o=%o x=%x s=%s\n", 10, 10, 10, 10, 10)
printf("0.04f=%0.04f : 0.02f=%0.02f\n", 10.12945, 10.12945)
10.129450
10.13
d=10 f=10.000000 o=12 x=a s=10
0.04f=10.1294 : 0.02f=10.13
开发环境:Rubymine
The book of Ruby----A hands-on guide for the Adventurous---[美]Huw Collingbourne---no starch press
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)