diff --git a/ChangeLog.d/bugfix/ZBX-25739 b/ChangeLog.d/bugfix/ZBX-25739
new file mode 100644
index 00000000000..b48851f6ca9
--- /dev/null
+++ b/ChangeLog.d/bugfix/ZBX-25739
@@ -0,0 +1 @@
+..F.....S. [ZBX-25739] fixed timeleft() function to return 1.7976931348623157E+308 instead of making item unsupported in the case of the infinite time (arimdjonoks, averza)
diff --git a/src/libs/zbxalgo/prediction.c b/src/libs/zbxalgo/prediction.c
index 916ed832ba8..8cc7f1c007b 100644
--- a/src/libs/zbxalgo/prediction.c
+++ b/src/libs/zbxalgo/prediction.c
@@ -1189,15 +1189,15 @@ out:
 	{
 		result = ZBX_MATH_ERROR;
 	}
+	else if (0.0 > result || DBL_MAX < result)
+	{
+		result = DBL_MAX;
+	}
 	else if (ZBX_IS_NAN(result))
 	{
 		zabbix_log(LOG_LEVEL_DEBUG, "numerical error");
 		result = ZBX_MATH_ERROR;
 	}
-	else if (0.0 > result || DBL_MAX < result)
-	{
-		result = DBL_MAX;
-	}
 
 	zbx_matrix_free(coefficients);
 
diff --git a/src/libs/zbxnum/num.c b/src/libs/zbxnum/num.c
index e6374e4b258..e853d480d86 100644
--- a/src/libs/zbxnum/num.c
+++ b/src/libs/zbxnum/num.c
@@ -165,7 +165,7 @@ int	zbx_double_compare(double a, double b)
 
 int	zbx_validate_value_dbl(double value)
 {
-	if (value < -1e+308 || value > 1e+308)
+	if (0 != isnan(value) || 0 != isinf(value))
 		return FAIL;
 
 	return SUCCEED;
diff --git a/tests/libs/zbxexpression/evaluate_function.yaml b/tests/libs/zbxexpression/evaluate_function.yaml
index d83abd30dde..0a49e3142a1 100644
--- a/tests/libs/zbxexpression/evaluate_function.yaml
+++ b/tests/libs/zbxexpression/evaluate_function.yaml
@@ -1502,6 +1502,29 @@ out:
   return: SUCCEED
   value: 13
 ---
+test case: Evaluate timeleft(5m,wait infinite)
+in:
+  history:
+  - itemid: 1
+    value type: ITEM_VALUE_TYPE_FLOAT
+    data:
+    - value: 1.6e+30
+      ts: 2017-01-10 10:01:00.000000000 +00:00
+    - value: 1.5e+30
+      ts: 2017-01-10 10:02:00.000000000 +00:00
+    - value: 1.4e+30
+      ts: 2017-01-10 10:03:00.000000000 +00:00
+    - value: 1.3e+30
+      ts: 2017-01-10 10:04:00.000000000 +00:00
+    - value: 1.2e+30
+      ts: 2017-01-10 10:05:00.000000000 +00:00
+  time: 2017-01-10 10:05:00.000000000 +00:00
+  function: timeleft
+  params: '5m,1.7e+30'
+out:
+  return: SUCCEED
+  value: 1.7976931348623158e+308
+---
 test case: Evaluate timeleft(5m,65)
 in:
   history:
@@ -1525,6 +1548,29 @@ out:
   return: SUCCEED
   value: 3600
 ---
+test case: Evaluate timeleft(5m,1.7976931348623158e+305)
+in:
+  history:
+  - itemid: 1
+    value type: ITEM_VALUE_TYPE_FLOAT
+    data:
+    - value: 1.1e+305
+      ts: 2017-01-10 10:01:00.000000000 +00:00
+    - value: 1.2e+305
+      ts: 2017-01-10 10:02:00.000000000 +00:00
+    - value: 1.3e+305
+      ts: 2017-01-10 10:03:00.000000000 +00:00
+    - value: 1.4e+305
+      ts: 2017-01-10 10:04:00.000000000 +00:00
+    - value: 1.5e+305
+      ts: 2017-01-10 10:05:00.000000000 +00:00
+  time: 2017-01-10 10:05:00.000000000 +00:00
+  function: timeleft
+  params: '5m,1.6e+305'
+out:
+  return: SUCCEED
+  value: 60
+---
 test case: Evaluate timeleft(5m,65,test)
 in:
   history:
diff --git a/ui/include/func.inc.php b/ui/include/func.inc.php
index acd76d20eae..e29104e0f72 100644
--- a/ui/include/func.inc.php
+++ b/ui/include/func.inc.php
@@ -1354,7 +1354,7 @@ function make_sorting_header($obj, $tabfield, $sortField, $sortOrder, $link = nu
 /**
  * Format floating-point number in the best possible way for displaying.
  *
- * @param float $number   Valid number in decimal or scientific notation.
+ * @param float $number   Valid floating point number.
  * @param array $options  Formatting options.
  *
  * $options = [
@@ -1370,14 +1370,6 @@ function make_sorting_header($obj, $tabfield, $sortField, $sortOrder, $link = nu
  * @return string
  */
 function formatFloat(float $number, array $options = []): string {
-	if ($number == INF) {
-		return _('Infinity');
-	}
-
-	if ($number == -INF) {
-		return '-'._('Infinity');
-	}
-
 	$defaults = [
 		'precision' => ZBX_FLOAT_DIG,
 		'decimals' => 0,
@@ -1394,75 +1386,75 @@ function formatFloat(float $number, array $options = []): string {
 		'zero_as_zero' => $zero_as_zero
 	] = $options + $defaults;
 
-	if ($zero_as_zero && $number == 0) {
-		return '0';
-	}
-
-	$number_original = $number;
+	$round_fn = static function (float $mantissa, int $exponent, int $decimals): array {
+		$mantissa_rounded = abs(round($mantissa, $decimals));
 
-	$exponent = (int) explode('E', sprintf('%.'.($precision - 1).'E', $number))[1];
-
-	if ($exponent < 0) {
-		for ($i = 1; $i >= 0; $i--) {
-			$round_precision = $decimals - $exponent - $i;
+		if ($mantissa_rounded >= 10) {
+			$mantissa_rounded = 1;
+			$exponent++;
+			$decimals++;
+		}
 
-			// PHP rounding bug when precision is set more than 294.
-			if ($round_precision > 294) {
-				$decimal_shift = pow(10, $round_precision - 294);
-				$test = round($number * $decimal_shift, 294) / $decimal_shift;
-			}
-			else {
-				$test = round($number, $round_precision);
-			}
+		$digits = $decimals >= 0
+			? rtrim(str_replace('.', '', sprintf('%.'.$decimals.'F', $mantissa_rounded)), '0')
+			: '';
 
-			$test_number = sprintf('%.'.($precision - 1).'E', $test);
-			$test_digits = $precision == 1
-				? 1
-				: strlen(rtrim(explode('E', $test_number)[0], '0')) - ($test_number[0] === '-' ? 2 : 1);
+		return [$digits, $exponent, $mantissa_rounded == abs($mantissa)];
+	};
 
-			if (!$small_scientific || $test_digits - $exponent < $precision) {
-				break;
-			}
-		}
-		$number = $test_number;
-		$digits = $test_digits;
-	}
-	else {
-		if ($exponent >= $precision) {
-			if ($exponent >= min(PHP_FLOAT_DIG, $precision + 3)
-					|| round($number, $precision - $exponent - 1) != $number) {
-				$number = round($number, $decimals - $exponent);
-			}
-		}
-		else {
-			$number = round($number, min($decimals, $precision - $exponent - 1));
+	$format_fn = static function (string $sign, string $integer, string $fraction, ?int $exponent = null)
+			use ($decimals, $decimals_exact): string {
+		if ($decimals_exact) {
+			$fraction = str_pad($fraction, $decimals, '0');
 		}
 
-		$number = sprintf('%.'.($precision - 1).'E', $number);
-		$digits = $precision == 1 ? 1 : strlen(rtrim(explode('E', $number)[0], '0')) - ($number[0] === '-' ? 2 : 1);
-	}
+		return $sign.$integer.($fraction !== '' ? '.'.$fraction : '').
+			($exponent !== null ? 'E'.($exponent >= 0 ? '+' : '').$exponent : '');
+	};
 
 	if ($zero_as_zero && $number == 0) {
 		return '0';
 	}
+	elseif ($number == INF) {
+		return _('Infinity');
+	}
+	elseif ($number == -INF) {
+		return '-'._('Infinity');
+	}
 
-	$exponent = (int) explode('E', sprintf('%.'.($precision - 1).'E', $number))[1];
+	$sign = $number < 0 ? '-' : '';
 
-	if ($exponent < 0) {
-		if (!$small_scientific
-				|| $digits - $exponent <= ($decimals_exact ? min($decimals + 1, $precision) : $precision)) {
-			return number_format($number, $decimals_exact ? $decimals : $digits - $exponent - 1, '.', '');
-		}
-		else {
-			return sprintf('%.'.($decimals_exact ? $decimals : min($digits - 1, $decimals)).'E', $number);
-		}
+	[$mantissa, $exponent] = explode('E', sprintf('%.'.(PHP_FLOAT_DIG - 1).'E', $number));
+
+	$mantissa = (float) $mantissa;
+	$exponent = (int) $exponent;
+
+	[$digits_dec, $exponent_dec, $precise_dec] = $round_fn($mantissa, $exponent,
+		$exponent >= 0
+			? min($exponent + $decimals, $precision - 1)
+			: ($decimals_exact
+				? $exponent + $decimals
+				: $decimals - 1
+			)
+	);
+
+	[$digits_sci, $exponent_sci] = $round_fn($mantissa, $exponent, $decimals);
+
+	if ($exponent_dec >= ($precise_dec ? min(PHP_FLOAT_DIG, $precision + 3) : $precision)
+			|| $small_scientific && (
+				$decimals_exact && $exponent_sci < -1 && -$exponent_sci + strlen($digits_sci) > $decimals + 1
+					|| !$decimals_exact && -$exponent_sci + strlen($digits_sci) > min(PHP_FLOAT_DIG, $precision + 3))) {
+		return $format_fn($sign, $digits_sci[0], substr($digits_sci, 1), $exponent_sci);
 	}
-	elseif ($exponent >= min(PHP_FLOAT_DIG, $precision + 3)
-			|| ($exponent >= $precision && $number != $number_original)) {
-		return sprintf('%.'.($decimals_exact ? $decimals : min($digits - 1, $decimals)).'E', $number);
+	elseif ($exponent_dec >= 0) {
+		return $format_fn($sign, str_pad(substr($digits_dec, 0, $exponent_dec + 1), $exponent_dec + 1, '0'),
+			substr($digits_dec, $exponent_dec + 1)
+		);
 	}
 	else {
-		return number_format($number, $decimals_exact ? $decimals : max(0, min($digits - $exponent - 1, $decimals)), '.', '');
+		return $digits_dec !== ''
+			? $format_fn($sign, '0', str_repeat('0', -$exponent_dec - 1).$digits_dec)
+			: $format_fn('', '0', '');
 	}
 }
 
diff --git a/ui/tests/unit/include/function_convertUnitsTest.php b/ui/tests/unit/include/function_convertUnitsTest.php
index 270b28b573d..5b1df6b16c6 100644
--- a/ui/tests/unit/include/function_convertUnitsTest.php
+++ b/ui/tests/unit/include/function_convertUnitsTest.php
@@ -92,7 +92,9 @@ class function_convertUnitsTest extends TestCase {
 			[ 'in' => ['value' => '1.00000012345', 'decimals' => 4],							'out' => '1' ],
 			[ 'in' => ['value' => '1.00000012345', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.0000'],
 			[ 'in' => ['value' => '0.00000012345', 'decimals' => 4],							'out' => '0.0000001235' ],
-			[ 'in' => ['value' => '0.00000012345', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2350E-7' ],
+			[ 'in' => ['value' => '0.00000012345', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2345E-7' ],
+			[ 'in' => ['value' => '0.000000123456', 'decimals' => 4],							'out' => '0.0000001235' ],
+			[ 'in' => ['value' => '0.000000123456', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2346E-7' ],
 
 			// Decimal units.
 
@@ -159,7 +161,9 @@ class function_convertUnitsTest extends TestCase {
 			[ 'in' => ['value' => '1.00000012345', 'units' => 'U', 'decimals' => 4],							'out' => '1 U' ],
 			[ 'in' => ['value' => '1.00000012345', 'units' => 'U', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.0000 U'],
 			[ 'in' => ['value' => '0.00000012345', 'units' => 'U', 'decimals' => 4],							'out' => '0.0000001235 U' ],
-			[ 'in' => ['value' => '0.00000012345', 'units' => 'U', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2350E-7 U' ],
+			[ 'in' => ['value' => '0.00000012345', 'units' => 'U', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2345E-7 U' ],
+			[ 'in' => ['value' => '0.000000123456', 'units' => 'U', 'decimals' => 4],							'out' => '0.0000001235 U' ],
+			[ 'in' => ['value' => '0.000000123456', 'units' => 'U', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2346E-7 U' ],
 			[ 'in' => ['value' => '1.5555E+22', 'units' => 'U', 'power' => 7],									'out' => '15.56 ZU' ],
 			[ 'in' => ['value' => '1.5555E+22', 'units' => 'U', 'power' => 8],									'out' => '0.016 YU' ],
 
@@ -228,7 +232,9 @@ class function_convertUnitsTest extends TestCase {
 			[ 'in' => ['value' => '1.00000012345', 'units' => 'B', 'decimals' => 4],							'out' => '1 B' ],
 			[ 'in' => ['value' => '1.00000012345', 'units' => 'B', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.0000 B' ],
 			[ 'in' => ['value' => '0.00000012345', 'units' => 'B', 'decimals' => 4],							'out' => '0.0000001235 B' ],
-			[ 'in' => ['value' => '0.00000012345', 'units' => 'B', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2350E-7 B' ],
+			[ 'in' => ['value' => '0.00000012345', 'units' => 'B', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2345E-7 B' ],
+			[ 'in' => ['value' => '0.000000123456', 'units' => 'B', 'decimals' => 4],							'out' => '0.0000001235 B' ],
+			[ 'in' => ['value' => '0.000000123456', 'units' => 'B', 'decimals' => 4, 'decimals_exact' => true],	'out' => '1.2346E-7 B' ],
 			[ 'in' => ['value' => '15728640', 'units' => 'B', 'power' => 2],									'out' => '15 MB' ],
 			[ 'in' => ['value' => '15728640', 'units' => 'B', 'power' => 3],									'out' => '0.015 GB' ],
 
@@ -287,9 +293,9 @@ class function_convertUnitsTest extends TestCase {
 			// Time units with decimals (convertUnitsSWithDecimals), ignore milliseconds.
 
 			[ 'in' => ['value' => '0.0012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => false, 'ignore_milliseconds' => true],	'out' => '0.001235s' ],
-			[ 'in' => ['value' => '0.0012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => true, 'ignore_milliseconds' => true],	'out' => '1.2350E-3s' ],
+			[ 'in' => ['value' => '0.0012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => true, 'ignore_milliseconds' => true],	'out' => '1.2345E-3s' ],
 			[ 'in' => ['value' => '0.00012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => false, 'ignore_milliseconds' => true],	'out' => '0.0001235s' ],
-			[ 'in' => ['value' => '0.00012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => true, 'ignore_milliseconds' => true],	'out' => '1.2350E-4s' ],
+			[ 'in' => ['value' => '0.00012345', 'units' => 's', 'decimals' => 4, 'decimals_exact' => true, 'ignore_milliseconds' => true],	'out' => '1.2345E-4s' ],
 			[ 'in' => ['value' => '0.00012', 'units' => 's', 'decimals' => 4, 'decimals_exact' => false, 'ignore_milliseconds' => true],	'out' => '0.00012s' ],
 			[ 'in' => ['value' => '0.00012', 'units' => 's', 'decimals' => 4, 'decimals_exact' => true, 'ignore_milliseconds' => true],		'out' => '1.2000E-4s' ],
 			[ 'in' => ['value' => '1e-100', 'units' => 's', 'decimals' => 4, 'decimals_exact' => false, 'ignore_milliseconds' => false],	'out' => '1E-97ms' ],
diff --git a/ui/tests/unit/include/function_formatFloatTest.php b/ui/tests/unit/include/function_formatFloatTest.php
index 2f9315294d1..590d52b10ca 100644
--- a/ui/tests/unit/include/function_formatFloatTest.php
+++ b/ui/tests/unit/include/function_formatFloatTest.php
@@ -26,7 +26,23 @@ class function_formatFloatTest extends TestCase {
 			[9.5,					[],	'10'],
 			[9.99999999999999E+14,	[],	'999999999999999'],
 			[9.99999999999999E+15,	[],	'1E+16'],
-
+			[PHP_FLOAT_MIN,			[],	'2E-308'],
+			[-PHP_FLOAT_MIN,		[],	'-2E-308'],
+			[PHP_FLOAT_MAX,			[],	'2E+308'],
+			[-PHP_FLOAT_MAX,		[],	'-2E+308'],
+
+			[0.1,					['decimals' => 2],	'0.1'],
+			[0.11,					['decimals' => 2],	'0.11'],
+			[0.111,					['decimals' => 2],	'0.11'],
+			[0.01,					['decimals' => 2],	'0.01'],
+			[0.011,					['decimals' => 2],	'0.011'],
+			[0.0111,				['decimals' => 2],	'0.011'],
+			[0.9,					['decimals' => 2],	'0.9'],
+			[0.99,					['decimals' => 2],	'0.99'],
+			[0.999,					['decimals' => 2],	'1'],
+			[0.09,					['decimals' => 2],	'0.09'],
+			[0.099,					['decimals' => 2],	'0.099'],
+			[0.0999,				['decimals' => 2],	'0.1'],
 			[0,						['decimals' => 4],	'0'],
 			[1.1,					['decimals' => 4],	'1.1'],
 			[1.0001,				['decimals' => 4],	'1.0001'],
@@ -40,6 +56,10 @@ class function_formatFloatTest extends TestCase {
 			[1E-15,					['decimals' => 4],	'1E-15'],
 			[1.0004E-14,			['decimals' => 4],	'1.0004E-14'],
 			[1.0005E-14,			['decimals' => 4],	'1.0005E-14'],
+			[PHP_FLOAT_MIN,			['decimals' => 2],	'2.23E-308'],
+			[-PHP_FLOAT_MIN,		['decimals' => 2],	'-2.23E-308'],
+			[PHP_FLOAT_MAX,			['decimals' => 2],	'1.8E+308'],
+			[-PHP_FLOAT_MAX,		['decimals' => 2],	'-1.8E+308'],
 
 			[1E+6,					['precision' => 4],	'1000000'],
 			[1E+7,					['precision' => 4],	'1E+7'],
@@ -49,15 +69,28 @@ class function_formatFloatTest extends TestCase {
 			[1.004E+100,			['precision' => 4, 'decimals' => 2],	'1E+100'],
 			[1.005E+100,			['precision' => 4, 'decimals' => 2],	'1.01E+100'],
 			[0.129,					['precision' => 4, 'decimals' => 2],	'0.13'],
-			[0.0129,				['precision' => 4, 'decimals' => 2],	'1.29E-2'],
-			[0.00129,				['precision' => 4, 'decimals' => 2],	'1.29E-3'],
-			[0.000129,				['precision' => 4, 'decimals' => 2],	'1.29E-4'],
+			[0.0129,				['precision' => 4, 'decimals' => 2],	'0.013'],
+			[0.00129,				['precision' => 4, 'decimals' => 2],	'0.0013'],
+			[0.000129,				['precision' => 4, 'decimals' => 2],	'0.00013'],
+			[0.0000129,				['precision' => 4, 'decimals' => 2],	'1.29E-5'],
 
 			[0.129,					['precision' => 4, 'decimals' => 2, 'decimals_exact' => true],	'0.13'],
 			[0.0129,				['precision' => 4, 'decimals' => 2, 'decimals_exact' => true],	'1.29E-2'],
 			[0.00129,				['precision' => 4, 'decimals' => 2, 'decimals_exact' => true],	'1.29E-3'],
 			[0.000129,				['precision' => 4, 'decimals' => 2, 'decimals_exact' => true],	'1.29E-4'],
 
+			[0.1,					['decimals' => 2, 'decimals_exact' => true],	'0.10'],
+			[0.11,					['decimals' => 2, 'decimals_exact' => true],	'0.11'],
+			[0.111,					['decimals' => 2, 'decimals_exact' => true],	'0.11'],
+			[0.01,					['decimals' => 2, 'decimals_exact' => true],	'0.01'],
+			[0.011,					['decimals' => 2, 'decimals_exact' => true],	'1.10E-2'],
+			[0.0111,				['decimals' => 2, 'decimals_exact' => true],	'1.11E-2'],
+			[0.9,					['decimals' => 2, 'decimals_exact' => true],	'0.90'],
+			[0.99,					['decimals' => 2, 'decimals_exact' => true],	'0.99'],
+			[0.999,					['decimals' => 2, 'decimals_exact' => true],	'1.00'],
+			[0.09,					['decimals' => 2, 'decimals_exact' => true],	'0.09'],
+			[0.099,					['decimals' => 2, 'decimals_exact' => true],	'9.90E-2'],
+			[0.0999,				['decimals' => 2, 'decimals_exact' => true],	'9.99E-2'],
 			[0,						['decimals' => 4, 'decimals_exact' => true],	'0'],
 			[0.0004,				['decimals' => 4, 'decimals_exact' => true],	'0.0004'],
 			[0.0005,				['decimals' => 4, 'decimals_exact' => true],	'0.0005'],
@@ -67,6 +100,10 @@ class function_formatFloatTest extends TestCase {
 			[100.0005,				['decimals' => 4, 'decimals_exact' => true],	'100.0005'],
 			[100.00004,				['decimals' => 4, 'decimals_exact' => true],	'100.0000'],
 			[100.00005,				['decimals' => 4, 'decimals_exact' => true],	'100.0001'],
+			[PHP_FLOAT_MIN,			['decimals' => 2, 'decimals_exact' => true],	'2.23E-308'],
+			[-PHP_FLOAT_MIN,		['decimals' => 2, 'decimals_exact' => true],	'-2.23E-308'],
+			[PHP_FLOAT_MAX,			['decimals' => 2, 'decimals_exact' => true],	'1.80E+308'],
+			[-PHP_FLOAT_MAX,		['decimals' => 2, 'decimals_exact' => true],	'-1.80E+308'],
 
 			[0,						['decimals' => 4, 'decimals_exact' => true, 'zero_as_zero' => false],	'0.0000'],
 			[1,						['decimals' => 4, 'decimals_exact' => true, 'zero_as_zero' => false],	'1.0000'],
@@ -77,6 +114,10 @@ class function_formatFloatTest extends TestCase {
 			[9.5,					['small_scientific' => false],	'10'],
 			[9.99999999999999E+14,	['small_scientific' => false],	'999999999999999'],
 			[9.99999999999999E+15,	['small_scientific' => false],	'1E+16'],
+			[PHP_FLOAT_MIN,			['small_scientific' => false],	'0'],
+			[-PHP_FLOAT_MIN,		['small_scientific' => false],	'0'],
+			[PHP_FLOAT_MAX,			['small_scientific' => false],	'2E+308'],
+			[-PHP_FLOAT_MAX,		['small_scientific' => false],	'-2E+308'],
 
 			[0,						['decimals' => 4, 'small_scientific' => false],	'0'],
 			[1.1,					['decimals' => 4, 'small_scientific' => false],	'1.1'],
