Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / float number

Author
Message
Allie
16
Years of Service
User Offline
Joined: 5th Jan 2008
Location: Melbourne, Aus
Posted: 24th Jan 2008 19:31
does anybody know how to take a float calclation like 4.5677543201 and get it to display 4.5, thanks
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 24th Jan 2008 21:36
This is one way:


LB
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th Jan 2008 00:10
Actually, (and I know I'm being pedantic), mathematically speaking, shouldn't rounding 4.5677543201 be 4.6 - or are you more interested in the length rather than the accuracy?

If length is the only factor, here's another way:



If accuracy is required you need a function. (LBFN's and my above alternative method both return 4.5 when rounding 4.57 so they are just working on length rather than proper rounding).

The following works OK, but there are probably much more efficient ways to do it.



Call it with:

A#=4.5677543201
Print Round(A#,1)


The 1 parameter is the number of places required.

TDK_Man

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 25th Jan 2008 05:12
Does this work? str$(4.5677543201,1)


Whatever...
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 25th Jan 2008 13:02
Yes, that will truncate and round a floating point number.




Come see the WIP!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Jan 2008 07:08
Doh! I should have guessed...

I actually booted up DBP and searched through the help files. I'd never have thought of looking to see if STR$() had had a decimal places parameter added!

Ah well... life's a bitch and then you die.

TDK_Man

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 26th Jan 2008 13:37
@TDK: Yup. I did the same thing last week. Same poster too. Small world.


Whatever...
Allie
16
Years of Service
User Offline
Joined: 5th Jan 2008
Location: Melbourne, Aus
Posted: 26th Jan 2008 18:46
Excelent thanks for the replys there great, the one from cash curtis II
How do you actualy get it to work? if you run with a wait key it displays an error saying it cannot find the function? thanks in advanse for any response, al.
bobba
16
Years of Service
User Offline
Joined: 10th Apr 2008
Location:
Posted: 26th Apr 2008 09:22
int temp;

float myfloat = 4.5677543201;

myfloat = myfloat*10;
temp = myfloat;
myfloat = temp/10;

//now myfloat = 4.5//


this is a simple way of doing it in c++, i do not know db but hope this helps. just change the 10s to 100s if you wanted it to end up as 4.65

Login to post a reply

Server time is: 2024-11-25 07:29:15
Your offset time is: 2024-11-25 07:29:15