简体   繁体   English

随着长度的变化删除十进制值中的尾随零

[英]Remove trailing zeros in decimal value with changing length

I have a procedure I am doing that displays odds but the client wants only significant digits to be shown.我正在执行一个显示赔率的程序,但客户只希望显示有效数字。 So, 1.50 would show as '1.5' and 1.00 would show as '1'.因此,1.50 将显示为“1.5”,而 1.00 将显示为“1”。

How can I get MySQL to not display trailing zeros;如何让 MySQL 不显示尾随零;

ie in the database:即在数据库中:

Odds赔率
1.500 1.500
23.030 23.030
2.000 2.000
4.450 4.450

would display as将显示为

1.5 1.5
23.03 23.03
2 2
4.45 4.45

Thanks for any help谢谢你的帮助

Easiest way by far, just add zero!迄今为止最简单的方法,只需添加零!

Examples:例子:

SET 
    @yournumber1="1.500", 
    @yournumber2="23.030",
    @yournumber3="2.000",
    @yournumber4="4.450"
;

SELECT 
    (@yournumber1+0),
    (@yournumber2+0),
    (@yournumber3+0),
    (@yournumber4+0)
;

+------------------+------------------+------------------+------------------+
| (@yournumber1+0) | (@yournumber2+0) | (@yournumber3+0) | (@yournumber4+0) |
+------------------+------------------+------------------+------------------+
|              1.5 |            23.03 |                2 |             4.45 |
+------------------+------------------+------------------+------------------+
1 row in set (0.00 sec)

If the column your value comes from is DECIMAL or NUMERIC type, then cast it to string first to make sure the conversion takes place...ex:如果您的值来自DECIMALNUMERIC类型的列,则首先将其转换为字符串以确保发生转换...例如:

SELECT (CAST(`column_name` AS CHAR)+0) FROM `table_name`;

For a shorter way, just use any built-in string function to do the cast:对于更短的方法,只需使用任何内置字符串函数来执行转换:

SELECT TRIM(`column_name`)+0 FROM `table_name`;

EDIT : I would use the answer below by Christopher McGowan instead - adding 0 to the value, which is better, instead.编辑:我会改用克里斯托弗·麦高恩 (Christopher McGowan) 的以下答案 - 将 0 添加到值中,这样更好。


It's important to check there is actually a decimal point if doing trimming.如果进行修整,检查实际上是否有小数点很重要。

So I think you'd want to use:所以我认为你想使用:

SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
FROM yourtable
WHERE yourfield LIKE '%.%'

this worked for me.. round the field to 2 decimal places and then trim any trailing zeros这对我有用..将字段舍入到小数点后两位,然后修剪任何尾随零

So that 2.10 is 2.1所以 2.10 是 2.1

SELECT trim(round(FIELDNAME,2))+0 
FROM tbl_name
....

To remove trailing zeros from a DECIMAL / NUMERIC or string type column, you can simply cast the value to DOUBLE , eg:要从DECIMAL / NUMERIC或字符串类型列中删除尾随零,您可以简单地将值转换为DOUBLE ,例如:

SELECT CAST(mycol AS DOUBLE) from mytable;

or或者

SELECT mycol + 0E0 FROM mytable;

In fact, the "cast to char and add zero" trick mentioned in other answers does the same, but in a more indirect (and likely less efficient) way, eg:事实上,其他答案中提到的“转换为字符并添加零”技巧也是如此,但以更间接(可能效率更低)的方式,例如:

SELECT CAST(mycol AS CHAR)+0 FROM mytable; -- converts to string, then to number
SELECT TRIM(mycol)+0 FROM mytable; -- ditto

Please use below function , it will take care of number having zero without decimal places ie 150 etc....请使用下面的功能,它会处理没有小数位的零数字,即150等......

 SET @saved_cs_client     = @@character_set_client;
 SET character_set_client = utf8;
 DELIMITER $$
 USE `mydbname`$$

 DROP FUNCTION IF EXISTS `FN_STRIP_TRAILING_ZER0`$$

 CREATE DEFINER=`mydbuser`@`%` FUNCTION `FN_STRIP_TRAILING_ZER0`(tNumber DECIMAL(10,7)) RETURNS VARCHAR(20) CHARSET utf8

 BEGIN
     DECLARE strBuff VARCHAR(20);
     DECLARE cnt  NUMERIC(2);
     DECLARE tString VARCHAR(20);
     SELECT CAST(tNumber AS CHAR) INTO tString;
     SELECT LOCATE('.',tString) INTO cnt;
     IF cnt > 0 THEN
       SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM tString)) INTO strBuff;   
     ELSE
       SET strBuff = tString;
     END IF;
     RETURN strBuff;
 END$$
 DELIMITER ;
 SET character_set_client = @saved_cs_client;

Addendum:附录:

Typically to call this would involve: SELECT FN_STRIP_TRAILING_ZER0(1.5);通常调用它会涉及: SELECT FN_STRIP_TRAILING_ZER0(1.5);

我发现的最佳解决方案是将您的回合值转换为 FLOAT:

SELECT CAST(ROUND(1.2345984372, 2) AS FLOAT)

Here's what worked for me:以下是对我有用的内容:

SINGLE COLUMN:单列:

SELECT TRIM(column_name)+0 AS column_name FROM table_name;

MULTIPLE COLUMNS:多列:

SELECT 
TRIM(column1)+0 AS column1,
TRIM(column2)+0 AS column2,   
TRIM(column3)+0 AS column3,
FROM table_name;

Taking forward the answer provided by @fooquency, if the column is already declared as a DECIMAL with a non-zero value for D in DECIMAL(M, D), we do not need to perform the WHERE condition转发@fooquency 提供的答案,如果该列已经声明为 DECIMAL,DECIMAL(M, D) 中的 D 值为非零,则我们不需要执行 WHERE 条件

WHERE yourfield LIKE '%.%'

as the values in the column will always contain D digits after the decimal dot (.)因为列中的值将始终在小数点 (.) 后包含 D 位数字

Try this simply updated Query to remove decimal zeros 试试这个简单更新的 Query来删除十进制零

SELECT TRIM(TRAILING '.00' FROM price_column)
FROM table_name

If you are using PHP as the scripting language you may use the following:如果您使用 PHP 作为脚本语言,您可以使用以下内容:

$var = (float)$var_having_extra_0; // $var = (float) 17.5000

Or use the PHP floatval function:或者使用 PHP floatval函数:

$var = floatval($var_having_extra_0); // $var = floatval(17.5000)

Using ROUND or CEILING, in the query you just have to type:使用 ROUND 或 CEILING,您只需在查询中键入:

SELECT ROUND(2/50) 

or或者

SELECT CEILING(2/50)

I had a similar problem in a situation where I could not modify the code nor the SQL query, but I was allowed to modify the database structure.我在无法修改代码或 SQL 查询的情况下遇到了类似的问题,但我可以修改数据库结构。 So I changed the column format from DECIMAL to FLOAT and it solved my problem.所以我将列格式从 DECIMAL 更改为 FLOAT 并解决了我的问题。

SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
FROM yourtable
WHERE yourfield LIKE '%.%'

or或者

SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
FROM yourtable
WHERE instr(yourfield,'.') != 0

work ok but require a "where" clause.工作正常,但需要一个“where”子句。

I think the best solution is probably:我认为最好的解决方案可能是:

SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM ROUND(yourfield,3))) 
FROM yourtable

as it doesn't require a "where" clause, doesn't require any special code, and also lets you set the maximum precision of the number upfront.因为它不需要“where”子句,不需要任何特殊代码,还可以让您预先设置数字的最大精度。

Taking fragments of the others answers in this page I came to this conclusion:从本页其他答案的片段中我得出了这个结论:

SELECT ( IF(
    myfield LIKE '%.%', 
    TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM myfield)),
    myfield
) ) FROM mytable

Cheers干杯

SELECT TRIM(TRAILING '0' FROM yourodds)
FROM ...

Docs for the TRIM function . TRIM 函数的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM