Go 实现判断变量是否为合法数字 IsNumeric 算法
【转】 http://www.syyong.com/Go/Go-to-determine-whether-the-variable-is-a-legal-digital-algorithm.html
IsNumeric — 检测变量是否为数字或数字字符串。
支持小数点、十六进制(hex)、科学计数法。
is_numeric
// is_numeric()
func IsNumeric(val interface{}) bool {
switch val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
case float32, float64, complex64, complex128:
return true
case string:
str := val.(string)
if str == "" {
return false
}
// Trim any whitespace
str = strings.Trim(str, " \\t\\n\\r\\v\\f")
if str[] == '-' || str[] == '+' {
if len(str) == {
return false
}
str = str[:]
}
// hex
if len(str) > && str[] == '' && (str[] == 'x' || str[] == 'X') {
for _, h := range str[:] {
if !((h >= '' && h <= '') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) {
return false
}
}
return true
}
// 0-9,Point,Scientific
p, s, l := , , len(str)
for i, v := range str {
if v == '.' { // Point
if p > || s > || i+ == l {
return false
}
p = i
} else if v == 'e' || v == 'E' { // Scientific
if i == || s > || i+ == l {
return false
}
s = i
} else if v < '' || v > '' {
return false
}
}
return true
} return false
}
Github地址
https://github.com/syyongx/php2go
Go 实现判断变量是否为合法数字 IsNumeric 算法的更多相关文章
- shell判断变量是字符还是数字
ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去 ...
- php判断变量是否为数字is_numeric()
is_numeric — 检测变量是否为数字或数字字符 <?php $tests = array( "31", 1380, "1e4", "no ...
- C# 判断一字符串是否为合法数字(正则表达式)
判断一个字符串是否为合法整数(不限制长度) public static bool IsInteger(string s) { string pattern = @"^\d*$"; ...
- str.isdigit()可以判断变量是否为数字
字符串.isdigit()可以判断变量是否为数字 是则输出True 不是则输出False 好像只能字符串
- Linux C判断日期格式是否合法
Title:Linux C判断日期格式是否合法 --2013-10-11 11:54 #include <string.h> // strlen() , strncpy() #includ ...
- Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字
本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- js 判断变量是否为空
js 判断变量是否为空 欢迎指正,补充! /** * 判断变量是否为空, * @param {[type]} param 变量 * @return {Boolean} 为空返回true,否则返回fal ...
- Oracle中如何判断字符串是否全为数字
Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...
随机推荐
- Linux CentOS7.0 (04)systemctl vs chkconfig、service
CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令 systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到 ...
- linux下的Shell编程(5)循环
Shell Script中的循环有下面几种格式: while [ cond1 ] && { || } [ cond2 ] -; do - done for var in -; do - ...
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.通过配置,我们配置某个 ViewResolver 如下: <b ...
- JavaScript中的 原型 property 构造函数 和实例对象之间的关系
1 为什么要使用原型? /* * javascript当中 原型 prototype 对象 * * */ //首先引入 prototype的意义,为什么要使用这个对象 //先来写一个构造函数的面向对象 ...
- show engine innodb status输出说明
参考链接 (https://dev.mysql.com/doc/refman/5.7/en/innodb-standard-monitor.html) 其中有这样一句: For a descripti ...
- Mysql 库表
create database student_info default character set utf8 collate utf8_general_ci; ------------------- ...
- mysql安装及常见问题
下载了MySQL的压缩包,开始配置的时候遇到一大堆问题,下面记录下,也希望对遇到同样问题的你有帮助 开始将压缩包解压到指定文件夹,然后建立一个txt文件命名为my.ini,写入下面的内容 [mysql ...
- requests+正则表达式提取猫眼电影top100
#requests+正则表达式提取猫眼电影top100 import requests import re import json from requests.exceptions import Re ...
- windows使用Win32DiskImager安装树莓派系统
首先去 官网 下载一个树莓派镜像. 然后使用Win32DiskImager这个工具安装. 不过试了以下好像不管用. 然后网上有ubuntu安装树莓派操作系统的方法. 于是就想我要是装树莓派不会还得装一 ...
- Sevrlet 工作原理解析-转
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...