Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Rounding Down in Tcl

Posted by eyarrow (eyarrow), 26 January 2004
Hi,

Is there any package I can call.... so that I can round down in Tcl as opposed to rounding up?

Many thanks

Elliot


Posted by admin (Graham Ellis), 26 January 2004
Hmm.  I thought Tcl rounded down ...

Code:
% expr 10/3
3
% expr 11/3
3
% expr 12/3
4
% expr 10./3
3.33333333333
% expr 11./3
3.66666666667
% expr -11/3
-4
%


All of these are rounding down except the 11./3 which has rounded up in about the 12th significant figure.

The -4 answer to the last test might be a suprise, but this is still rounding down.

Can you post a more specific example of the sort of behaviour that you want to change, and I'll answer further

Posted by eyarrow (eyarrow), 26 January 2004
Graham,

Many thanks.. what I am refering to is as follows:-

If we wish to round to 3 DP.... then normal rounding is as follows

1.64354          =>   1.644

If we were to round down to 3Dp... then it would be as follows

1.64354          =>  1.643

I believe, this is called ROUND_HALF_DOWN in Java.

Many thanks

Elliot

Posted by admin (Graham Ellis), 26 January 2004
Easiest way is to use the floor function, but that rounds down to the nearest whole number.   Here's a definition of a proc that does want you want (shifts your value, floors it, shifts it back)

Code:
proc round_down {val rounder} {
       set nval [expr floor($val*$rounder) /$rounder]
       return $nval
       }


Demo of that in use:

Code:
% source ndp.tcl
% round_down 123.456789 1000
123.456
%




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho