Code: Select all
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
print money/100 'show it as dollars
next i
sleep
Code: Select all
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
print money/100 'show it as dollars
next i
sleep
Code: Select all
'inspired by http://www.dreael.ch/Deutsch/BASIC-Knowhow-Ecke/Gleitkommazahlen.html
'a 2 digit behind point rounding function
function Float2String(value as double) as string
function="0"
dim as integer l
dim as string vstring,newstring
vstring = str(int(100! * abs(value) + .5))
l=len(vstring)
if l=1 then newstring ="0.0" & vstring
if l=2 then newstring ="0." & vstring
if l>2 then newstring =left(vstring,l-2) & "." & right(vstring,2)
'if right(newstring,3)=".00" then newstring=left(newstring,len(newstring)-3)'
if sgn(value)=-1 then newstring ="-" & newstring
function=newstring
end function
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
print money, money/100, float2string(money/100) 'show it as dollars
next i
sleep
Code: Select all
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
dim as double m = money/100
dim as integer s = sgn(m)
print s*int(s*m); using ".##"; s*m - int(s*m)
next i
sleep
Code: Select all
#include "crt.bi"
print 9.70
printf("%f",9.70)
sleep
Code: Select all
#include "crt.bi"
function CRound(byval x as double,byval precision as integer=2) as string
if precision>30 then precision=30
dim as zstring * 40 z:var s="%." &str(abs(precision)) &"f"
sprintf(z,s,x)
var ans = rtrim(rtrim(z,"0"),"."),zeros="",i=instr(ans,".")
if i then zeros= string(precision-(len(ans)-i),"0")
return ans+zeros
end function
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
print cround(money/100),money/100 'show it as dollars
next i
sleep
What exactly does that mean? A new sinister way to rip us all off?dodicat wrote:The C function will use banker's rounding.
Questions about Bankers from sb named "MrSwiss" ? :-)MrSwiss wrote:What exactly does that mean? A new sinister way to rip us all off?dodicat wrote:The C function will use banker's rounding.
(That's what bankers usually do, all day long ;-) , except of course, earning far too much money.)
The routine does, but the code before (that I quoted) already rounds a decimal number to a binary floating point. The same goes for the formating routine sprintf that uses binary floats internally.dodicat wrote:The float representation is not good.
dim as integer money
for i as integer = 0 to 20
money = int(rnd(1)*100000)/100 'create a random amount in cents
print cround(money/100),money/100 'show it as dollars
next i
sleep
[/code]
P.S.
The C function will use banker's rounding.
Well, as you might know:marcov wrote:Questions about Bankers from sb named "MrSwiss" ? :-)
I'm from the Netherlands, but not from Holland. There is an huge difference. Unfortunately can't say too loud, since that would be a bit pot and kettle, coming from a region where all German speaking people are routinely referred to as "Prüsse"MrSwiss wrote:Well, as you might know:marcov wrote:Questions about Bankers from sb named "MrSwiss" ? :-)
not everybody in Holland/Netherlands
Since I'm not from Edam, but from Limburg, so I OBVIOUSLY make Limburger cheese :-) (mainly between the toes though)is able to make "Edammer Cheese", grow Flowers etc.
I wasn't too seriously either, neither in this message. Unfortunately it is all true though :-)The question was meant to be a joke ... more than anything else.
I forgot to mention the main reason for why one would try to avoid (binary) floating point for monetary amounts at all costs:Thanks for the detailed answer anyway!
I've had my first encounter with accountants before even starting with anything IT related (as a Field-Technician for some machinery).marcov wrote:Sooner or later in your IT career you will be in a situation where you bill your customers, and then you'll have to deal with accountants. Accountants are strange folk, and they can really be obsessed endlessly about a discrepancy of a few cents in their balances.
Worse, they can ruin your perfectly fine after hours drink in the pub, by jabbering on about it endlessly!
Code: Select all
dim as integer money
dim as string strMoney
money = 45
strMoney = str(money)
print left(strMoney,len(strMoney)-2);".";right(strMoney,2)
sleep