View previous topic :: View next topic 
Author 
Message 
Kiltro_Chilensis General User
Joined: 06 Jun 2004 Posts: 45 Location: Proudly located in Chile

Posted: Thu Aug 19, 2004 1:41 pm Post subject: Decimal to Fractions 


I want to convert a number such as 0.75 to 3/4 how you do that?
I mean converting decimal to fraction.
PLEASE HELP! 

Back to top 


pitonyak Administrator
Joined: 09 Mar 2004 Posts: 3655 Location: Columbus, Ohio, USA

Posted: Thu Aug 19, 2004 5:36 pm Post subject: 


The entire solution is in my macro document in section 5.27 Decimal Feet To Fraction.
Note that this is the free document... _________________ 
Andrew Pitonyak
http://www.pitonyak.org/oo.php 

Back to top 


Kiltro_Chilensis General User
Joined: 06 Jun 2004 Posts: 45 Location: Proudly located in Chile

Posted: Thu Aug 19, 2004 7:03 pm Post subject: 


FOLLOW NEXT MESSAGE
Last edited by Kiltro_Chilensis on Thu Aug 19, 2004 8:40 pm; edited 1 time in total 

Back to top 


Kiltro_Chilensis General User
Joined: 06 Jun 2004 Posts: 45 Location: Proudly located in Chile

Posted: Thu Aug 19, 2004 8:37 pm Post subject: 


could you check this code, please?
Quote: 
function Fractionizer(decimal as double) as string
dim x, y, a, b as integer 'a and b are auxiliars
y=10^len(right(decimal,len(decimal)instr(decimal,"."))
x=val(right(decimal,len(decimal)instr(decimal,"."))
a = x
b = y
while x<>y
if x > y then x=xy
if x < y then y=yx
wend
Fractionizer = cstr(a/x) + " / " + cstr(b/x)
end function 
I tested and it worked with small decimals, any suggestion?
EL KILTRO CHILENO 

Back to top 


SergeM Super User
Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France

Posted: Thu Aug 19, 2004 10:42 pm Post subject: 


Try this function (slighty changed)
Code: 
function Fractionizer(decimal as double) as string
dim x, y, a, b as integer 'a and b are auxiliars
y=10^len(right(decimal,len(decimal)instr(decimal,"."))
'x=val(right(decimal,len(decimal)instr(decimal,"."))
x= decimal*y 'changed
'a=x changed
a = decimal*y
b = y
while x<>y
if x > y then x=xy
if x < y then y=yx
wend
Fractionizer = cstr(a/x) + " / " + cstr(b/x)
end function

and says me if this is what you want ? _________________ Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide 

Back to top 


Kiltro_Chilensis General User
Joined: 06 Jun 2004 Posts: 45 Location: Proudly located in Chile

Posted: Fri Aug 20, 2004 12:14 pm Post subject: 


I was getting some overflow problems so I changed ( in RED ), the data type from x, y, a, b from integer to double
Quote: 
function Fractionizer(decimal as double) as string
dim x, y, a, b as double 'a and b are auxiliars
y=10^len(right(decimal,len(decimal)instr(decimal,"."))
x= decimal*y 'changed, you were right
a=x ' not changed, 'cause is the same with less code
b = y
while x<>y
if x > y then x=xy
if x < y then y=yx
wend
Fractionizer = cstr(a/x) + " / " + cstr(b/x)
end function

EL KILTRO CHILENO 

Back to top 


pitonyak Administrator
Joined: 09 Mar 2004 Posts: 3655 Location: Columbus, Ohio, USA

Posted: Sat Aug 21, 2004 11:49 am Post subject: 


Unfortunately, I am out of town at the moment so I can only work from memory, but I am surprised that you do not simply pull the complete working example that already exists. That said, I also know the value of "rolling your own".
Let me ask a question since I can not run and test the code. What toes the following line of code really do?
Code:  dim x, y, a, b as double 
If you said that this line of code declares three variables of type variant and one variable of type double then you are correct. Remember to declare the type for each individual variable. _________________ 
Andrew Pitonyak
http://www.pitonyak.org/oo.php 

Back to top 


Kiltro_Chilensis General User
Joined: 06 Jun 2004 Posts: 45 Location: Proudly located in Chile

Posted: Sat Aug 21, 2004 12:09 pm Post subject: 


"even the most greatest hunters have lost the rabit"
I was assuming that you can put each variable name and at the end you put the data type...
thanks... 

Back to top 


