标签: numeric

仅检查数字和一个可选小数点的字符串.

我需要检查一个字符串是否只包含数字.我怎样才能在C#中实现这一目标?

string s = "123"    ? valid 
string s = "123.67" ? valid 
string s = "123F"   ? invalid 
Run Code Online (Sandbox Code Playgroud)

有没有像IsNumeric这样的功能?

c# numeric

7
推荐指数
1
解决办法
2万
查看次数

有没有一种简单的方法可以将序数字符串转换为匹配的数值?

有没有人知道将"第一","第十"和"第一百"之类的单词转换为数字等价的方法?

样品:"第一" - > 1,"第二" - > 2,"第十" - > 10,"百分之一" - > 100

任何算法都足够了,但我在C#中写这个.

编辑

它不漂亮,一次只能用一个单词,但它适合我的目的.也许有人可以改进它,但我没时间.

 public static int GetNumberFromOrdinalString(string inputString)
    {
        string[] ordinalNumberWords = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth" };
        string[] ordinalNumberWordsTens = { "", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" };
        string[] ordinalNumberWordsExtended = {"hundredth", "thousandth", "millionth", "billionth" };

        if (inputString.IsNullOrEmpty() || inputString.Length < 5 || inputString.Contains(" …
Run Code Online (Sandbox Code Playgroud)

c# string algorithm ordinals numeric

7
推荐指数
1
解决办法
1601
查看次数

Android EditText默认数字键盘并允许文本

可能重复:
默认情况下带有数字键盘的EditText,但允许使用字母字符

想知道是否可以在编辑文本字段的焦点上默认使用数字键盘,但也允许用户输入字符?没有任何输入类型似乎支持这一点.谢谢!

keyboard android input numeric android-edittext

7
推荐指数
1
解决办法
3760
查看次数

将数字字符串编码为缩短的字母数字字符串,然后再返回

快问.我试图在Python中查找或编写一个编码器,通过使用大写和小写字母缩短一串数字.数字字符串看起来像这样:

20120425161608678259146181504021022591461815040210220120425161608667
Run Code Online (Sandbox Code Playgroud)

长度总是一样的.

我最初的想法是编写一些简单的编码器来利用大写和小写字母和数字来缩短这个字符串,看起来更像是这样:

a26Dkd38JK
Run Code Online (Sandbox Code Playgroud)

这完全是武断的,只是想尽可能清楚.我确信有一种非常光滑的方式可以做到这一点,可能已经内置.也许这是一个令人尴尬的问题甚至要问.

此外,我需要能够采取缩短的字符串并将其转换回更长的数值.我应该写一些东西并发布代码,还是我应该已经知道的这是一个用Python函数构建的一行?

谢谢!

python string encode numeric

7
推荐指数
2
解决办法
5811
查看次数

在R中没有NA强制将字符转换为数字

我在R中工作并且有一个带有数字向量的数据帧dd_2006.当我第一次导入数据时,我需要从3个变量中删除$,小数点和一些空格:SumOfCost,SumOfCases和SumOfUnits.为此,我用过str_replace_all.但是,一旦我使用str_replace_all,矢量被转换为字符.所以我使用as.numeric(var)将向量转换为数字,但引入了NAs,即使我在运行as.numeric代码之前运行下面的代码时,向量中也没有NA.

sum(is.na(dd_2006$SumOfCost))
[1] 0
sum(is.na(dd_2006$SumOfCases))
[1] 0
sum(is.na(dd_2006$SumOfUnits))
[1] 0
Run Code Online (Sandbox Code Playgroud)

这是导入后的代码,从向量中删除$开始.在str(dd_2006)输出中,我为了空间而删除了一些变量,因此str_replace_all下面代码中的列#s 与我在此处发布的输出不匹配(但它们在原始代码中执行):

library("stringr")
dd_2006$SumOfCost <- str_sub(dd_2006$SumOfCost, 2, ) #2=the first # after the $

#Removes decimal pt, zero's after, and commas
dd_2006[ ,9] <- str_replace_all(dd_2006[ ,9], ".00", "")
dd_2006[,9] <- str_replace_all(dd_2006[,9], ",", "")

dd_2006[ ,10] <- str_replace_all(dd_2006[ ,10], ".00", "")
dd_2006[ ,10] <- str_replace_all(dd_2006[,10], ",", "")

dd_2006[ ,11] <- str_replace_all(dd_2006[ ,11], ".00", "")
dd_2006[,11] <- str_replace_all(dd_2006[,11], ",", "")

str(dd_2006)
'data.frame':   12604 obs. …
Run Code Online (Sandbox Code Playgroud)

r numeric vector character na

7
推荐指数
2
解决办法
7万
查看次数

Decimal.MinValue&Decimal.MaxValue:为什么静态只读而不是const修饰符?

在C#中,MinValue字段是为数字类型定义的:

① 十进制类型的静态只读修饰符(链接到MSDN Libray for .NET 4.5):

public static readonly decimal MinValue
Run Code Online (Sandbox Code Playgroud)

② 所有其他数字类型的const修饰符:

//Integral signed numeric types
public const sbyte MinValue
public const short MinValue
public const int MinValue
public const long MinValue

//Integral unsigned numeric types
public const byte MinValue
public const ushort MinValue
public const uint MinValue
public const ulong MinValue

//Real numeric types
public const float MinValue
public const double MinValue
Run Code Online (Sandbox Code Playgroud)

为什么const修饰符不用于定义Decimal.MinValue字段?

备注:

①同样的问题适用于数值类型MaxValue …

static types const numeric readonly

7
推荐指数
1
解决办法
2172
查看次数

无法准确计算Python上的pi

我是这里的新成员,我会直接进入这个,因为我整个星期天都试图绕过它.

我是Python的新手,以前在C++上学习了基础中级编码(这是一个为期10周的大学模块).

我正在尝试一些迭代技术来计算Pi,但两者都有些不准确,我不知道为什么.

我在大学教的第一种方法 - 我相信你们中的一些人之前已经看过它.

x=0.0
y=0.0
incircle = 0.0
outcircle = 0.0
pi = 0.0
i = 0
while (i<100000):
    x = random.uniform(-1,1)
    y = random.uniform(-1,1)
    if (x*x+y*y<=1):
        incircle=incircle+1
    else:
        outcircle=outcircle+1
    i=i+1
pi = (incircle/outcircle)
print pi
Run Code Online (Sandbox Code Playgroud)

它本质上是两个轴上-1到+1平面上随机(x,y)坐标的生成器.然后,如果x ^ 2 + y ^ 2 <= 1,我们知道该点位于由坐标轴形成的框内的半径为1的圆内.

根据点的位置,计数器增加incircleoutcircle.

然后,pi的值是圆圈内外的值的比率.坐标是随机生成的,所以它应该是均匀的.

但是,即使在非常高的迭代值下,我对Pi的结果总是在3.65左右.

第二种方法是另一次迭代,它计算多边形的周长,边数增加,直到多边形几乎是一个圆,然后,Pi =圆周/直径.(我有点被骗,因为编码有一个math.cos(Pi)术语,所以看起来我正在使用Pi来找到Pi,但这只是因为你不能轻易地使用度来表示Python上的角度).但即使是高迭代,最终结果似乎也在3.20左右结束,这也是错误的.代码在这里:

S = 0.0
C = 0.0
L = 1.0

n = 2.0
k = 3.0
while (n<2000):
    S = 2.0**k
    L = L/(2.0*math.cos((math.pi)/(4.0*n))) …
Run Code Online (Sandbox Code Playgroud)

python pi numeric montecarlo python-2.7

7
推荐指数
1
解决办法
282
查看次数

Rust中数字功能的特性

是否有任何特征指定一些数字功能?我想用它来绑定泛型类型,就像这个假设HasSQRT:

fn some_generic_function<T>(input: &T)
    where T: HasSQRT
{
    // ...
    input.sqrt()
    // ... 
}
Run Code Online (Sandbox Code Playgroud)

numeric traits generic-programming rust

7
推荐指数
1
解决办法
5870
查看次数

用C和Fortran中的Leibniz系列计算Pi

我正在尝试比较C和Fortran代码的性能。为了使用Leibniz的系列计算pi ,我获得了以下Fortran代码

program pi_leibniz
implicit none

    integer, parameter :: dp=selected_real_kind(15,307)
    integer :: k=0, precision=9
    real(dp), parameter :: correct = 0.7853981633974483d0, eps = epsilon(real(1,dp)) 
    real(dp) :: sum = 0.0, delta
    character(8) :: fmt
    logical, parameter :: explicit = .false.
    real :: start, finish

    delta = 10.**(-precision-1)*0.25
    if (delta<eps) then
        delta=eps
        precision=14
        print *, "Precision specified too high, reverting to double precision (14 digits)"
    endif

    write(fmt,'(A,I0,A,I0,A)') '(f',precision+2,'.',precision,')'

    call cpu_time(start)

    do
        sum = sum + real((-1)**k,dp)/real(2*k+1,dp)
        k = k+1
        if (abs(sum-correct)<delta) …
Run Code Online (Sandbox Code Playgroud)

c fortran numeric

7
推荐指数
1
解决办法
201
查看次数

perl6函数层对数字和字符串的作用不同吗?

快速提问:

-1.9.floor给您-1,而“ -1.9” .floor给您-2。应该是这样吗?对我来说似乎有点矛盾。

> say -1.9.floor
-1
> say "-1.9".floor
-2
Run Code Online (Sandbox Code Playgroud)

文档说“将其向下舍入到最接近的整数”。两者都应该为-2吗?

谢谢!!!

numeric floor perl6

6
推荐指数
1
解决办法
111
查看次数