Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 6th, 2003, 12:18 AM
 genedavinci
I am still learning

Join Date: Apr 2002
Location: Kuala Lumpur, Malaysia
Posts: 249
Time spent in forums: 43 m 2 sec
Reputation Power: 12
Getting rids of unwanted decimals

I am having problem to solve this simple problem of mine. Like what the subject said, I am trying to get rid of the trailing decimals for my output.

What I want:

input = 111.975
output = 111

What I don't want:

input = 111.975
output = 112

I've tried:

CAST(input AS INT)
TO_CHAR(input,'99999999')

Both fail to give me the output I want. How can I solve this decimal problem of mine?

Cheers.
__________________
I have yet to find the right way to do it.

www.genedavinci.com

#2
February 6th, 2003, 03:43 AM
 a.koepke
Second highest poster :p

Join Date: Jul 2001
Posts: 7,322
Time spent in forums: 8 h 13 m 55 sec
Reputation Power: 32
You are thinking in the wrong way, treat it as a string not a number ;-)

substring(fieldname from 1 for position('.' in fieldname))

See how that works. I dont know how it will cope if there isnt a decimal.
__________________
- Andreas Koepke

Koepke Photography

#3
February 6th, 2003, 12:09 PM
 imind
Registered User

Join Date: Aug 2002
Location: mexico
Posts: 13
Time spent in forums: 6 m 44 sec
Reputation Power: 0
This should also work:
round( 111.795, 0 )

#4
February 6th, 2003, 12:21 PM
 imind
Registered User

Join Date: Aug 2002
Location: mexico
Posts: 13
Time spent in forums: 6 m 44 sec
Reputation Power: 0
I wrote round(111.795,0).
It is more like trunc(111.795,0).

#5
February 7th, 2003, 02:15 AM
 genedavinci
I am still learning

Join Date: Apr 2002
Location: Kuala Lumpur, Malaysia
Posts: 249
Time spent in forums: 43 m 2 sec
Reputation Power: 12
it works!

however, what does '0' do in trunc(111.975,0)?

i find that trunc(111.975) itself will work without the additional '0'.

cheers.

#6
February 7th, 2003, 12:45 PM
 imind
Registered User

Join Date: Aug 2002
Location: mexico
Posts: 13
Time spent in forums: 6 m 44 sec
Reputation Power: 0
There are two variants for trunc, one specifies the number of decimal places.

trunc(111.795)=111
trunc(111.795,0)=111
trunc(111.795,2)=111.79

You'll find this on the Postgresql 7.3.1 User's Guide ->Functions and Operators->Math Functions.

#7
February 7th, 2003, 10:20 PM
 genedavinci
I am still learning

Join Date: Apr 2002
Location: Kuala Lumpur, Malaysia
Posts: 249
Time spent in forums: 43 m 2 sec
Reputation Power: 12
thanks!

 Viewing: Dev Shed Forums > Databases > PostgreSQL Help > Getting rids of unwanted decimals