# Easiest way to cut off decimals from a float?

 08 August 2012 DeadlyNightshade Plz...   portfolio Martin Dahlin Jr. Technical Artist King Stockholm, Sweden Easiest way to cut off decimals from a float? I've made a procedure that calculates the distance between two UV points using pythagora's theorem, and then it converts that number into pixels and lists this information on a few strings. Problem is that the variables I have with the results (all floats) usually have HUGE amounts of decimals after them, and I want to display only two decimal numbers. Now I've searched around the net a bit and doing this seems A LOT harder than it needs to be: MEL doesn't even have a function for this like C++ or well... any programming language really. So what method is the best (least complex - least amount of code) for just cutting away a few decimals from a float? (not round up!) share quote
 08 August 2012 backwheelbates Frequenter   Eric Bates Senior Animator Image Engine Vancouver, Canada This is a bit ridiculous, but it works. ``````proc float cutDecimals(float \$number) { return (float(int(\$number*100))/100); } cutDecimals(21.1984561);`````` share quote
 08 August 2012 rgkovach123 Know-it-All robert austin, USA you could use a regular expression to return any number of numbers, a period, and then two numbers. might be a bit complicated to get the regex setup, buts its very little code. look at the 'match' and 'subsitute' commands. share quote
 08 August 2012 backwheelbates Frequenter   Eric Bates Senior Animator Image Engine Vancouver, Canada Good idea rgkovach123 Try this one: `````` float \$number = 3548.651566; match "[0-9]+[.]*[0-9][0-9]" (string(\$number)); // Result: 3548.65 // `````` Last edited by backwheelbates : 08 August 2012 at 09:42 PM. share quote
 08 August 2012 zeroeffect Code Junkie portfolio Chris Zurbrigg Software Developer Independent Canada Similar to backwheelbates: ``````\$myFloat = 1.32323223; \$myFloat = `trunc(\$myFloat * 100)` / 100;`````` Last edited by zeroeffect : 08 August 2012 at 12:37 PM. share quote
 08 August 2012 Mark-J Expert   portfolio Mark Jackson CEO & Founder Red9 Consultancy United Kingdom could always just cast it over to Python: \$a=1.32323223; python ("'%0.2f' % ("+ \$a +")"); where 0.2 is 2 decimal places __________________ Founder & CEO : Red9Consultancy Red9 StudioPack : Red9 StudioPack GitHub Author: Autodesk Masterclass 2011 - 'Live Animation Binding' share quote
 08 August 2012 DeadlyNightshade Plz...   portfolio Martin Dahlin Jr. Technical Artist King Stockholm, Sweden Thanks for all the answers guys. Got lots of different methods to chose from now. share quote
 08 August 2012 CGTalk Moderation Expert Thread automatically closed This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum. __________________ CGTalk Policy/Legalities Note that as CGTalk Members, you agree to the terms and conditions of using this website. share quote

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off CGSociety Society of Digital Artists www.cgsociety.org Powered by vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.