百科狗-知识改变命运!
--

floatval() - php 变量处理函数

百变鹏仔1年前 (2023-11-21)阅读数 14#技术干货
文章标签变量

floatval()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

获取变量的浮点值

描述

floatval(mixed $var):float

floatval() - php 变量处理函数

返回变量$var的float数值。

$var可以是任何标量类型。你不能将floatval()用于数组或对象。

参见intval()、strval()、settype()和类型戏法。

This function takes the last comma or dot (if any) to make a clean float, ignoring thousand separator, currency or any other letter :
function tofloat($num) {
  $dotPos = strrpos($num, '.');
  $commaPos = strrpos($num, ',');
  $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : 
    ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
  
  if (!$sep) {
    return floatval(preg_replace("/[^0-9]/", "", $num));
  } 
  return floatval(
    preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
    preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
  );
}
$num = '1.999,369€';
var_dump(tofloat($num)); // float(1999.369)
$otherNum = '126,564,789.33 m²';
var_dump(tofloat($otherNum)); // float(126564789.33)
Demo : http://codepad.org/NW4e9hQH
you can also use typecasting instead of functions:
(float) $value;
To view the very large and very small numbers (eg from a database DECIMAL), without displaying scientific notation, or leading zeros.
FR : Pour afficher les très grand et très petits nombres (ex. depuis une base de données DECIMAL), sans afficher la notation scientifique, ni les zéros non significatifs. 
Float value less than 0.0001 (0.0000999999999999995) will be converted by floatval to scientific notation (exponential notation): 
More elegant function with selection of decimal point (deafault ,): 
locale aware floatval: 
There is much easier way to deal with formatted numbers:

double(13232.95)
The last getFloat() function is not completely correct.
1.000.000 and 1,000,000 and its negative variants are not correctly parsed. For the sake of comparing and to make myself clear I use the name parseFloat in stead of getFloat for the new function:

Comparing of float parsing functions with the following function: 
Most of the functions listed here that deal with $ and , are unnecessarily complicated. You can use ereg_replace() to strip out ALL of the characters that will cause floatval to fail in one simple line of code: 
(float) would be more performant here (up to 6x times faster).
intval, floatval, doubleval, strva for PHP4 functions (intval, floatval, doubleval, strval), in PHP5 use type casting construction (i.e. '(type) parameter').
Be aware the last tofloat($num).
In theory it is very useful to have a function "separator-agnostic" (I think "locale based" solutions are useless if you have to parse a user file that can have a locale different to the server).
But this can lead to misinterpretations; in short: "123,456" is "123.456" (so comma used as decimal separator) or "123456" (comma used as thousand separator).
In any case, if you really want to use it, please don't forget that this function doesn't manage negative numbers.
I get the following disturbing results:
var_dump string(10) "0.01333"
echo the string=0.01333
echo (float)string=0
echo floatval(string)=0
The string is an outcome of array_map('str_getcsv', file(...
I can't find the characters 8-10
thanks
@pillepop2003 at yahoo dot de

use: "/^[0-9-]*[\.]{1}[0-9-]+$/"
instead of: "/^[0-9]*[\.]{1}[0-9-]+$/"
Use this snippet to extract any float out of a string. You can choose how a single dot is treated with the (bool) 'single_dot_as_decimal' directive.
This function should be able to cover almost all floats that appear in an european environment.

Big Up.
Philipp
Instead of using floatval which only appeared in PHP 4.2 you could juse use $variable = (float)$variable
This function doesn't seem to add any functionality that wasn't already there.
setlocale() and floatval() duo could break your DB queries in a very simple way:

You would need simple workaround like: 
i noticed all (well, unless i missed something) the functions working with decimals destroy trailing decimal places. this function restores them in case you want to be able to display a consistent precision for users. 
This function converts a string to a float no matter is the decimal separator dot (.) or comma (,). It also converts integers correctly. It takes the digits from the beginning of the string and ignores all other characters.

-Zipi (Finland)
For those of you, who are looking for a function that rips the first,
but longest possible float (or at least integer) from a string,
like 123.45 from the string "Price: 123,45$"
If no useable value is found, the function returns false.
Checks for both comma and dot as decimal-separator,
but does not check for 3 digits between thousands,
so 1,234.5 is as valid as 1,23,4.5 (both will return 1234.5)
12,.3 will return 12
1,000,000 will return 1000.0 !
(if thousands separator is defined,
decimals should be defined too ... 
in fact I was too lazy to check for that too)
Here you go, and feel free to optimize the function ;) 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)