skip to Main Content

When I use these selections I have zeros after comma, how can I get the result I expect

select concat(5678.0/1000,'');

select (5678.0/1000)::text;

both gives : 5.6780000000000000

what i need is this : 5.678

3

Answers


  1. kindly run this:

    SELECT ROUND(5678.0/1000, 3);
    
    Login or Signup to reply.
  2. rtrim the unwanted trailing decimal positions. A check is needed if the number has decimal point so that you do not eliminate relevant zeroes (as in 1000)

    with dt as (
    select (5678.0/1000)::text num union all
    select (5678.901/1000)::text union all
    select (1000)::text
      )
    select 
      num,
      case when num like '%.%' then rtrim(num,'0') else num end num2
    from dt
    
    num                 num2
    5.6780000000000000  5.678
    5.6789010000000000  5.678901
    1000                1000
    
    Login or Signup to reply.
  3. You can use TRIM(TRAILING FROM <string>, <chars>). For example:

    select trim(trailing from ('' || 1560.0 / 1000.0), '0'); -- 1.56
    
    select trim(trailing from ('' || 5678.0 / 1000.0), '0'); -- 5.678
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search