【转】 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 算法的更多相关文章

  1. shell判断变量是字符还是数字

    ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去 ...

  2. php判断变量是否为数字is_numeric()

    is_numeric — 检测变量是否为数字或数字字符 <?php $tests = array( "31", 1380, "1e4", "no ...

  3. C# 判断一字符串是否为合法数字(正则表达式)

    判断一个字符串是否为合法整数(不限制长度) public static bool IsInteger(string s) { string pattern = @"^\d*$"; ...

  4. str.isdigit()可以判断变量是否为数字

    字符串.isdigit()可以判断变量是否为数字 是则输出True 不是则输出False 好像只能字符串

  5. Linux C判断日期格式是否合法

    Title:Linux C判断日期格式是否合法 --2013-10-11 11:54 #include <string.h> // strlen() , strncpy() #includ ...

  6. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  7. Delphi 判断一个字符串是否为数字

    //函 数 名: IsDigit//返 回 值: boolean//日       期:2011-03-01//参       数: String//功       能: 判断一个字符串是否为数字// ...

  8. js 判断变量是否为空

    js 判断变量是否为空 欢迎指正,补充! /** * 判断变量是否为空, * @param {[type]} param 变量 * @return {Boolean} 为空返回true,否则返回fal ...

  9. Oracle中如何判断字符串是否全为数字

    Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...

随机推荐

  1. python全栈开发-json和pickle模块(数据的序列化)

    一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...

  2. leetcode算法: Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  3. IdentityServer4-介绍大纲(译文)

    简介 IdentityServer4是一个基于ASP.NET CORE2使用OAuth2.0协议和OpenID Connect的框架 特性如下: Authentiaction作为一个Service 集 ...

  4. 【SQL.基础构建-第二节(2/4)】

    --      Tips:查询基础 --一.SELECT 语句基础-- 1.查询指定列:SELECT 关键字--语法:--SELECT <列名>, ...     -- 希望查询列的名称- ...

  5. [LeetCode] Toeplitz Matrix 托普利兹矩阵

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  6. [LeetCode] My Calendar I 我的日历之一

    Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...

  7. nginx 安装及简单配置(适用 小白)

    一.nginxNginx是一个异步框架的 Web服务器,也可以用作反向代理,负载平衡器 和 HTTP缓存,Nginx可以部署在网络上使用FastCGI脚本.SCGI处理程序.WSGI应用服务器或Phu ...

  8. jdk 动态代理源码分析

    闲来无事,撸撸源码 使用方法 直接看代码吧.. package com.test.demo.proxy; import java.lang.reflect.InvocationHandler; imp ...

  9. Mlecms 反射型xss && 后台任意文件下载

    应该算0day吧,自己分析出来的,有点鸡肋,不过小cms分析确实比较简单. xss地址:search.php?word=a><img+src=1+onerror=alert`1`>a ...

  10. vba打开输入文件

    Sub fileCreate2() Dim folderPath, fileName, s As String Dim fs, fo, fc, f As Object folderPath = &qu ...