Easiest way to cut off decimals from a float?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  08 August 2012
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!)
  08 August 2012
This is a bit ridiculous, but it works.

proc float cutDecimals(float $number)
  return (float(int($number*100))/100);
  08 August 2012
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.
  08 August 2012
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.
  08 August 2012
Similar to backwheelbates:

$myFloat = 1.32323223;
$myFloat = `trunc($myFloat * 100)` / 100;

Last edited by zeroeffect : 08 August 2012 at 12:37 PM.
  08 August 2012
could always just cast it over to Python:

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'
  08 August 2012
Thanks for all the answers guys. Got lots of different methods to chose from now.
  08 August 2012
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.
Thread Closed share thread

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
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 11:48 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.