Rounding (up/down) to a certain number of decimal places

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hello Group

How do I round say 1.23 up/down to 2 decimal places? I am currently
(ab)using [format].

Thanks
Evilson
0
Reply Evil 10/16/2010 8:45:41 AM

See related articles to this posting


Evil Son <ewilsonmail@gmail.com> writes:

> How do I round say 1.23 up/down to 2 decimal places? I am currently
> (ab)using [format].

Sounds like the right choice.

-- 
Donald Arseneau                          asnd@triumf.ca
0
Reply Donald 10/16/2010 10:24:21 AM

On 16 Pa=C5=BA, 10:45, Evil Son <ewilsonm...@gmail.com> wrote:
> Hello Group
>
> How do I round say 1.23 up/down to 2 decimal places? I am currently
> (ab)using [format].

Using [format] is completly correct way, but if you prefer to avoid it
for some reason, there is other way:

set precision 2
set roundedValue [expr { double(round($value * pow(10,$precision))) /
pow(10,$precision) }]

Regards,
Googie
0
Reply pawelsalawa (93) 10/16/2010 2:08:44 PM

On Oct 17, 12:08=C2=A0am, Googie <pawelsal...@gmail.com> wrote:
> On 16 Pa=C5=BA, 10:45, Evil Son <ewilsonm...@gmail.com> wrote:
>
> > Hello Group
>
> > How do I round say 1.23 up/down to 2 decimal places? I am currently
> > (ab)using [format].
>
> Using [format] is completly correct way, but if you prefer to avoid it
> for some reason, there is other way:
>
> set precision 2
> set roundedValue [expr { double(round($value * pow(10,$precision))) /
> pow(10,$precision) }]
>
> Regards,
> Googie

Thanks Donald and Googie ... least of all for figuring out what I
meant. My example should have been 1.234 :P
Yes [format] works fine on my machine but I was just wondering about
whether it would behave the same on other machines and whether that
was the correct way to do it. Sounds like it is. (IIRC, C's <stdio.h>
printf's behaviour is undefined here. But this is Tcl of course ...
and my C is rusty.)

Thanks again.
Evilson
0
Reply ewilsonmail (97) 10/16/2010 4:10:40 PM
comp.lang.tcl 22082 articles. 12 followers. Post

3 Replies
1161 Views

Similar Articles

[PageSpeed] 39


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Rounding to a certain number of decimal places
Hi, I would like to be able to round a number to a certain, specified number of decimal places (not number of digits), including zeros. Suppose that some function f does this, and takes two arguments, the number to be rounded and the number of decimal places to which to round: f[number, numPlaces]. Then, for example, I would like the following results, if possible: f[0.8168, 3] = 0.817 f[1.8168, 3] = 1.817 f[10.8168, 3] = 10.817 f[100.8168, 3] = 100.817 f[0.99, 3] = 0.990 f[0.9, 3] = 0.900 f[0.00572, 3] = 0.006 Is there a function f (either built-in or a custom function) ...

Round up a number to so many decimal places
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to be able to do it to 2 decimal places. Thanks for any help Scott. "Gizmo" <scottamillard@hotmail.com> wrote in message news:bist4t$4fn$1@newsg4.svr.pol.co.uk... > Hi there i was wondering if any one new to a function that rounds up a float > to so many decimal places. I have a number in bytes and converting it to > mb's and gb's but once its converted i ...

help rounding numbers to decimal places
Hi all, My name is George Erick Tasso I am new to lisp as well as this list. Could anyone kindly tell me how to round a number to any number of decimal places ? 5.123452 - > round to 2 decimal place would be 5.12 many thanks <gtasso@rbv.gov.vu> wrote in message news:1165364837.032657.48280@80g2000cwy.googlegroups.com... > Hi all, > My name is George Erick Tasso I am new to lisp as well as this list. > Could anyone kindly tell me how to round a number to any number of > decimal places ? > > 5.123452 - > round to 2 decimal place would be 5.12 Lisp is alive an...

Round a number to 1 decimal place
Hi, Let say I am doing a calculation A = B * C, and the answer of the result is A = 49.995. Is there any way in Matlab that I can round up the value of A to 50.0 (corr. to 1 d.p.)?? Thanks, Alan "Alan" <twaleung@engmail.uwaterloo.ca> wrote in message news:eeeb477.-1@webx.raydaftYaTP... > Hi, > > Let say I am doing a calculation A = B * C, and the answer of the > result is A = 49.995. Is there any way in Matlab that I can round up > the value of A to 50.0 (corr. to 1 d.p.)?? > > Thanks, > > Alan r = 0.1; % Resolution A=r*round(A/r); "Ala...

rounding number to two decimal places
hi! i want to know about a function that rounds the number to 2/n decimal places e.g. 2.346 should become 2.35 thanx fa "fatima madni" <fatimahgull@yahoo.com> wrote in message news:eed81ab.-1@webx.raydaftYaTP... > hi! > > i want to know about a function that rounds the number to 2/n decimal > places > > e.g. 2.346 should become 2.35 > > thanx > fa If you want the actual number in the workspace rounded, use: >> x=2.346 x = 2.346 >> (round(100.*x))/100 ans = 2.35 If you want the number to be ...

Rounding a Number to 1 place of decimals
Hi Everyone, How can I change a number 4.567889 to it's corresponding number 4.6 correct to one place of decimals? Thanks Finn FM wrote: > > > Hi Everyone, > > How can I change a number 4.567889 to it's corresponding number 4.6 > correct to one place of decimals? > > Thanks > > Finn Hi, Just to clarify some things. I want to use the number that I round for further calculations and therefore cannot use the format command. Finn FM <finnmckenna@yahoo.co.uk> wrote: > FM wrote: >> >> >> Hi Everyone, >> >> How can I...

Rounding a Number to a String with N decimal places
When rounding (or truncating) a number which may be either positive or negative, it is sometimes necessary to round either towards plus infinity or towards minus infinity. But those are special cases. Normally, however, rounding should be symmetrical about zero; rounding X should give the same digits as rounding -X, for all X. Some of the signed routines which have been quoted here and elsewhere are unsymmetrical. -- � John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 � <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript <URL...

Ingres II 2.6 ABF: Rounding a number to four decimal places
Hi. I would like to know how to round a number to the fourth decimal place in Ingres ABF. Kind Regards, Jason Tepoorten CLAS Configuration Manager TAS System Coordinator Department of Natural Resources and Mines Level 8 Land Centre Corner Mains Rd and Vulture Street WOOLLOONGABBA Q 4102 Australia ************************************************************************ The information in this e-mail together with any attachments is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any form of re...

Code cuts 0's after the last integer to the right of the decimal place when a set number of decimal places is needed
Hello, I found the solution to saving a cell array that contains both strings and numbers to an ASCII file @: http://www.mathworks.com/support/solutions/en/data/1-1190ZB/index.html?solution=1-1190ZB and I am using the following code: ==================================================== % Lat File generation NEED TO ADD DATE! exlat={lat_dat(1,3),lat_dat(1,1),0.01,sta_nme,'LAT','DATE'}; ex2 = cellfun(@ex_func,exlat,'UniformOutput',0); size_ex2 = cellfun(@length,ex2,'UniformOutput',0); str_length = max(max(cell2mat(size_ex2))); ex3 = cell...

Formatting a number, placing the decimal in the proper place
Can someone help me out on formatting a number and getting the decimal in the right place? 1) I have the number 8883. 2) I would like to format the number so it shows up as 88.33. Thanks for any support, past, present, and future... -- BoomerangThree On Thu, 19 Aug 2004 21:43:15 -0400, BoomerangThree wrote: Hi BoomerangThree.. Is there some more significance to that name? The 'Boomerang', I understand, since I have already seen this exact same post on c.l.j.help, more commonly referred to 'round here as multi-posting, further details.. <http://www.physci.org/code...

Calculate the precision of a floating point number (ie: the number of decimal places)
Hi all I'm trying to write a reliable and efficient library function that will calculate the precision of an input number (floating point). Please excuse my lame mathematics skills! Here is my first attempt at a function: int dec_places(double d) { int places = 0; while (!feq(d - (int)(d + 0.5), 0.0)) // where feq returns true if inputs differ by less than epsilon 0.0000005 { d *= 10; ++places; } return places; } The problem I have with the above solution is that it is possible to get caught in an infinite loop...

How to round to 6 decimal places at back? Matlab shows 4 decimal at back
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear X=[234 432 104 456 123 654 765 799 999 432 890 432 654 678 864 990 776 234 432 104 456 123 654 765 799 999 432 890 432 654 678 864 990 776 112 356 987 740 234 809 432 456 654 765 799 999 432 654 678 864 990 408 776 112 356 112 356 987 740 234 809 432 456 654 765 799 999 432 654 678 864 990 408 776 112 356]; XSize =size(X); Y=(X/999-1.5) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Matlab shows 4 decimal at back. Output Program: Y = -1.2658 -1.0676 -1.3959 -1.0435 -1.3769 -0.8453 -0.7342 -0.7002 -0.5000 -1.0676 -0.60...

number of decimal places
Hi, Is there any way to output a double value with different number of decimal places (apart from using 'sprintf'). I looked up the reference for C++ streams and found the 'setprecision' manipulator, but this sets the number of digits before and after the decimal point. I can't seem to find any manipulator for setting the number of digits after the decimal point. Thanks, -shez- Sorry guys, i've found the answer. Pls ignore. Sorry, -shez- On 19 Jan 2005 19:11:42 -0800, shez <shezanbaig2004@gmail.com> wrote: > Sorry guys, i've found the answer. Pl...

decimal places with numbers
In a table there is a field called concentration. It is a Number field. The numbers vary from 0.025 to 200 When I look at the table in a record whose concetration field has a value of 0.025, I see 0.03. If I click on it it shows me the 0.025. No problems there. In my code I have the following: NuConcentration = TC.Concentration If I view nuConcetration I see 0.03 More importantly, when the math is done (numbers X numbers) it is as if it 0.03. I need the precision. Any suggestions? Thanks, Craig Craig Futterman > In my code I have the following: > NuConcentration = T...

Round to two decimal place
Hi I have a variable say @price. It is either blank or has a value. If it is blank I have to show it like $ 0.00 . So I wrote helper like @price.blank? ? '$'+'0.00' : '$'+@price.to_s But the problem if @price has value 400.00 , after doing @price.to_s it changes to 400.0 What I want is exactly 400.00 .And if I remove to_s above it will throw an error can't convert BigDecimal into String Please help Thanks Tom -- Posted via http://www.ruby-forum.com/. On Tue, Sep 7, 2010 at 4:16 PM, Tom Mac <to.tom.mak@gmail.com> wro...

round on defined decimal place
Hello everybody, I want to round on defined decimal places. I'll be glad, if somebody could help me soon. Example: 1.23456789 1.23 Thanks in advance. Hi, search in the File Exchange for "round" <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4261&objectType=file> <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=6077&objectType=file> J�r�me peter wrote: > Hello everybody, > > I want to round on defined decimal places. I'll be glad, if somebody > could help me soon. > > Exampl...

Rounding up to 2 decimal places
Hi, What a nightmare i've been trawling deja all morning for a simple method of rounding a number up to 2 decimal places. I've checked out the ceil function which only rounds numbers up to no decimal places which is no good. The printf function rounds numbers up and down. Let's take this number 0.3325 I need it to be, 0.34 How do i do that. Do I have to write a routine that checks if there is a third digit or what. The number is calculated for the UK VAT system at 17.5% i need the number rounded up to only 2 decimal places. Yes I have checked perlfaq4. Simple problem se...

number of decimal places in a double
hi I am trying this calculation double a=2.2; double b=1.8; double sum=a+b; double diff=a-b; double remainder=a%b; when I print the values of these, I get a=2.2 b=1.8 sum=4.0 diff=0.40000000000000013 remainder=0.3999999999999988 In my calculations, I want to use diff as 0.4 instead of 40000000000000013 .Also I would like to get remainder as 0.4 instead of 0.3999999999999988. Is there some way to do this using any java api? thanks harry. harryos wrote: > hi > I am trying this calculation > > double a=2.2; > double b=1.8; > double sum=a+b; &...

rounding a float to on decimal place
Apologies if this is a daft question but is it possible to round a float to one decimal place , say 3.4467823 to 3.4. Math.round returns the next int. I kinda need the one decial place Thanks On Jan 27, 9:21 am, "Damo" <cormacdeba...@gmail.com> wrote: > ...is it possible to round a > float to one decimal place , say 3.4467823 to 3.4. See <http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html> HTH Andrew T. Hi, Damo wrote: > Apologies if this is a daft question but is it possible to round a > float to one decimal place , say 3.4467823 to 3....

How to round to specified number of decimals?
How do I round this number: 20.12453 to 20.1? or 20.12 more generally how do you specify number of decimals when rounding? saneman wrote: > How do I round this number: > > 20.12453 > > to > > 20.1? > > or 20.12 > > more generally how do you specify number of decimals when rounding? bob = roundn(20.12453,-1) gives: 20.1000 but how do I delete the trailing zeros? In article <fmkrs4$mi3$1@news.net.uni-c.dk>, saneman <yyyy@dd.com> wrote: >How do I round this number: >20.12453 >to >20.1? >or 20.12 You cannot. There i...

how to round to 2 decimal places?
Hey, I have been trying to find a way to round any value to 2 decimal places but ni such function seems to exist. I have tried round() but it only rounds to the nearest integer.Is there any way I can round any value to 2 decimal places.(even truncate will do). eg. function(3.14678)=3.15 if I use a round function or function (3.14678)=3.14 if I use a truncate function. Thanks for your help. I really appreciate it. kazim wrote: > Hey, > > I have been trying to find a way to round any value to 2 decimal > places but ni such function seems to exist. I have tried round() but &...

Rounding to the 10th decimal place
I have a large array with doubles and when I add them all up to compute the average similarities I get -1.QNAN error. Which I believe means overflow, so I would like to know how to round off these doubles to the 10th deciminal place using C++? Does it have a built in function? "ryanarossi@gmail.com" <ryanarossi@gmail.com> writes: > I have a large array with doubles and when I add them all up to compute > the average similarities I get -1.QNAN error. Which I believe means > overflow, so I would like to know how to round off these doubles to the > 10th deciminal pl...

Controlling number of decimal places
Say I have a DOUBLE variable that holds 200.23234 for example. I want to output this number to a text file as a number rounded to two decimal places. In other words, I want to output to a text file "200.23". How do I do this? I've looked into the ROUND function but only rounds to the nearest integer. Thanks for any help! Edwin Edwin writes: > Say I have a DOUBLE variable that holds 200.23234 for example. I want > to output this number to a text file as a number rounded to two > decimal places. In other words, I want to output to a text file > "200.23"...

round number at 2 decimal
using aggregate i am computing a MEAN with number with 2 decimal. The result end up with number something equal to xxxx.xxxxxxxxxxx with more then 10 decimals. I would like to cut it or round it at 2 decimal. I have use format like (F8.2) but the number still have with more then 10 decimals. Thanks On Wed, 30 Sep 2009 08:00:47 -0700 (PDT), otolithe <michel.simoneau@envill.com> wrote: >using aggregate i am computing a MEAN with number with 2 decimal. >The result end up with number something equal to xxxx.xxxxxxxxxxx with >more then 10 decimals. >I would like to ...