ruby 将字符转数字计算

计算位数 (Counting the number of digits)

Ruby does not provide you any predefined direct method through which you can find the number of digits in a number. Though one method can be implemented by converting the number into a string and then apply .length method to it. In that case, the expression will look like “number.to_s.length” but that requires conversion of number into a string. If you do not want to apply such type of method then you can go for the code given below.

Ruby没有为您提供任何预定义的直接方法,通过该方法可以找到数字中的数字位数 。 尽管可以通过将数字转换为字符串然后对它应用.length方法来实现一种方法。 在这种情况下,表达式将看起来像“ number.to_s.length”,但这需要将数字转换为字符串。 如果您不想应用此类方法,则可以使用下面给出的代码。

Methods used:

使用的方法:

  • puts: This method is a predefined method which is used to print a string on the console.

    puts :此方法是预定义的方法,用于在控制台上打印字符串。

  • gets: The gets method is used to get a string from the user through the console.

    gets :gets方法用于通过控制台从用户获取字符串。

Variables used:

使用的变量:

  • num: This variable is storing the number which is provided by the user.

    num :此变量存储用户提供的数字。

  • temp: This is acting as the temporary variable which is storing the value available in num.

    temp :这是一个临时变量,用于存储num中可用的值。

  • count: This is acting as a counter variable. It is storing the number of digits available in num.

    count :这是一个计数器变量。 它存储num中可用的位数。

Ruby代码来计算数字中的位数 (Ruby code to count the number of digits in a number)

=begin
Ruby program to count the number of digits
=end
puts "Enter the number:"
num=gets.chomp.to_i
temp=num
count=0
while (temp>0)
count+=1
temp=temp/10
end
puts "#{num} has #{count} digits"

Output

输出量

RUN 1:
Enter the number
89744
89744 has 5 digits
RUN 2:
Enter the number
8171627331
8171627331 has 10 digits

Code explanation:

代码说明:

You can observe in the above code that we have taken a variable count, which is behaving as a counter and counting the number of digits. The count is initialized by 0 and is incrementing when the condition mentioned with while loop is coming out to be true. The number is getting divided by 10 after every iteration. The code is not complex and is very easy to understand.

您可以在上面的代码中观察到我们采用了一个可变的count ,它作为一个计数器并在计数位数。 计数由0初始化,并在使用while循环提及的条件成立时递增。 每次迭代后,该数字将除以10。 代码并不复杂,很容易理解。

翻译自: https://www.includehelp.com/ruby/count-the-number-of-digits-in-a-number.aspx

ruby 将字符转数字计算

ruby 将字符转数字计算_Ruby程序计算一个数字中的位数相关推荐

  1. ruby打印_Ruby程序打印一个数字的乘法表

    ruby打印 打印乘法表 (Printing multiplication table) This requires a very simple logic where we only have to ...

  2. 将一个数组中的字符串用指定字符分割开,分别放到另一个数组中

    #include "stdafx.h" #include <stdio.h> #include <string.h> #pragma warning(dis ...

  3. java句子中找单词_java程序找到一个句子中每个单词的字母数

    我想找到一个句子中每个单词的字母数.我已经尝试了几次使用几个代码,但从来没有得到它.我总是显示错误StringIndexOutOfBounds异常.我的一个代码如下:java程序找到一个句子中每个单词 ...

  4. 利用Python语言编程,完成猜数游戏,系统随机产生一个1到100的数字num1,用户输入一个数字guess,如果没有猜对,根据系统给出的提示重新猜数,直到才对为止。

    利用Python语言编程,完成猜数游戏,系统随机产生一个1到100的数字num1,用户输入一个数字guess,如果没有猜对,根据系统给出的提示重新猜数,直到才对为止. 要求:(1)系统每次都要给出新的 ...

  5. js 实现2的n次方计算函数_js 判断一个数字是不是2的n次方幂的实例

    昨天去面试时,面试官问了一道面试题,说如何判断一个数是不是2的n次方幂,我当时不知道2的n次方幂是什么(糗大发了),还好给我解释了一下.最后回家上网查查资料,整理了一下方法. 方法一 如何判断一个数是 ...

  6. python输入一个字符串、计算其中小写字符的个数_编写程序,输入一个字符串,统计其中大小写字母数字和其他符号的个数并输出,要求统计过程在函数COUNT中进行...

    答:对比ASCII码,在程序中小写字母96 答:a = "aAsmr3idd4bgs7Dlsf9eAF" 请将a字符串的数字取出,并输出成一个新的字符串. 请统计a字符串出现的每个 ...

  7. java猜数字游戏应用程序_猜数字游戏的Java小程序

    /* 猜数字游戏: 1,产生随机数. 2,获取键盘录入. 3,将录入数据变成数字,和随机数比较. 给出提示信息. 4,重复这个过程,如果猜中,程序就结束. 注意:对于输入1~100以外的数字,,以及非 ...

  8. mysql的int多少溢出_mysql 整型(int)数字溢出在程序和数据库设计中的考虑

    以下试以整型(int)抛砖引玉: 一:MySQL5 以MySQL5版本为例,大多数管理员可能把自增数字.或者其它应用数字字段的列属性设置为int类型,int占用4个字节,而int又分为无符号型和有符号 ...

  9. java控制台计算数字_java从控制台接收一个数字

    //时间:2017/7/22 //作者:江骆 //功能:从控制台接收一个数 import java.io.*;  //引入一个IO流的包 public class helloworld1 { publ ...

最新文章

  1. epub阅读器_推荐一款Epub(windows版)阅读软件,附下载安装教程
  2. 《集体智慧编程》第六章
  3. mysql 添加唯一索引_浅谈Mysql索引
  4. 30 CO配置-控制-产品成本控制-成本对象控制-期末结算-定义更新
  5. 告别.net ! 2年多了,舍不得!
  6. 【BZOJ1014】【tyvj3486】火星人prefix,Splay+字符串hash
  7. VS2008下用MFC 的MSComm控件编写串口程序
  8. 百度搜索(URL)中汉字转为什么编码问题
  9. 计算机说课教案模板,说课稿通用模板
  10. ubuntu 服务器 ip修改,修改Ubuntu server ip地址
  11. CodeForces 760B Frodo and pillows
  12. 【云图】如何制作AMF生态鱼缸实体店分布图
  13. layui菜鸟教程--乐字节前端
  14. 【全国大学生IoT设计竞赛】安谋科技灵动赛题国赛一等奖分享:多足仿生机器人
  15. mysql数据库报 请在mysql配置文件修sql-mode为NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  16. python 遥感图像分类
  17. android 带头像的弹幕,原生Canvas实现带头像的弹幕
  18. 全球及中国HDPE电熔配件行业规模预测及十四五动态发展研究报告2021-2027年版
  19. win7 修改服务器属性,win7打印机服务器属性设置
  20. 《安士全书》原文及白话版

热门文章

  1. php+mysql记事本_一个简单记事本php操作mysql辅助类创建
  2. python123测验9程序题答案_Django ORM 练习题及答案_python_脚本之家
  3. java版spring cloud+spring boot 社交电子商务平台:服务消费(基础)
  4. AMD推出7nm高端显卡Radeon VII,直指英伟达RTX 2080
  5. 蚂蚁金服亿级并发下的移动端到端网络接入架构解析
  6. 团队作业8—团队项目用户验收评审
  7. JavaWeb应用配置文件安全解决方案
  8. CCNA 学习笔记(四)--路由协议(RIP)
  9. 机器翻译软件从实验室走向市场
  10. 系统容灾备份选型的决策表