Convert from string to numberic

瀏覽次數:4,991 次
跳到第一則未讀訊息

DươngQ Nguyễn

未讀,
2016年5月11日 凌晨4:19:462016/5/11
收件者:robotframework-users
Hi all,
  I cannot convert a String to Numberic. Like as:

  ${numb} = Convert to Number  '50,000'

The problem is '50,000'. It is a input value.

Pls, show me your suggestions
Thanks you!

Heiko Thiery

未讀,
2016年5月11日 凌晨4:31:432016/5/11
收件者:duong...@gmail.com、robotframework-users
You can simply replace the ',' with nothing...

${numb}=  Replace String  ${numberString}  ,  ${EMPTY}
${numb}=  Convert To Number  ${numb}

-
Heiko

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Pekka Klärck

未讀,
2016年5月12日 凌晨4:29:472016/5/12
收件者:Heiko T、DươngQ Nguyen、robotframework-users
2016-05-11 11:31 GMT+03:00 Heiko Thiery <heiko....@gmail.com>:
> You can simply replace the ',' with nothing...
>
> ${numb}= Replace String ${numberString} , ${EMPTY}
> ${numb}= Convert To Number ${numb}

The above works great if `,` is a thousands separator as at least in
the US. For example here in Finland `,` is a decimal separator,
though, and the above would yield totally wrong results. In here you
you would want to convert `,` into `.` instead.

Because handling different localized formats can be complicated, it
would actually be nice if `Convert To Number/Integer` would accept the
locale to use as an optional argument. Python's locale module makes
that possible, but that requires temporarily setting the global locale
setting that is not generally recommended [1]. If we make this
optional and document possible problems, it shouldn't be too big a
problem, though. Additionally, Robot probably should configure the
locale module to use users default locale when the program starts [2].
After that parsing numbers using the default locale would be safe.

[1] https://docs.python.org/2/library/locale.html#background-details-hints-tips-and-caveats
[2] https://docs.python.org/2/library/locale.html#locale.setlocale

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org
回覆所有人
回覆作者
轉寄
0 則新訊息