OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Decimal to Fractions

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Kiltro_Chilensis
General User
General User


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

PostPosted: Thu Aug 19, 2004 1:41 pm    Post subject: Decimal to Fractions Reply with quote

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
View user's profile Send private message
pitonyak
Administrator
Administrator


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

PostPosted: Thu Aug 19, 2004 5:36 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Kiltro_Chilensis
General User
General User


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

PostPosted: Thu Aug 19, 2004 7:03 pm    Post subject: Reply with quote

FOLLOW NEXT MESSAGE

Last edited by Kiltro_Chilensis on Thu Aug 19, 2004 8:40 pm; edited 1 time in total
Back to top
View user's profile Send private message
Kiltro_Chilensis
General User
General User


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

PostPosted: Thu Aug 19, 2004 8:37 pm    Post subject: Reply with quote

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=x-y
if x < y then y=y-x
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
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Thu Aug 19, 2004 10:42 pm    Post subject: Reply with quote

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=x-y
if x < y then y=y-x
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
View user's profile Send private message Visit poster's website
Kiltro_Chilensis
General User
General User


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

PostPosted: Fri Aug 20, 2004 12:14 pm    Post subject: Reply with quote

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=x-y
if x < y then y=y-x
wend
Fractionizer = cstr(a/x) + " / " + cstr(b/x)
end function


EL KILTRO CHILENO
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


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

PostPosted: Sat Aug 21, 2004 11:49 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Kiltro_Chilensis
General User
General User


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

PostPosted: Sat Aug 21, 2004 12:09 pm    Post subject: Reply with quote

Embarassed "even the most greatest hunters have lost the rabit" Embarassed

I was assuming that you can put each variable name and at the end you put the data type...

thanks...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group