Post Reply 
Convert decimal > fraction in program
12-09-2015, 08:23 PM (This post was last modified: 12-09-2015 08:36 PM by jrozsas.)
Post: #1
Convert decimal > fraction in program
In a program, what is the way to convert decimal to fraction and display it on the screen? Example: 5.25 = 21/4.
The key [Image: EFLhQ.jpg] has a command for programming?

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-09-2015, 09:01 PM (This post was last modified: 12-09-2015 09:09 PM by Marcus von Cube.)
Post: #2
RE: Convert decimal > fraction in program
exact(5.25) in CAS returns 21/4.

Edit: Works in Home as well.

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
12-09-2015, 10:56 PM
Post: #3
RE: Convert decimal > fraction in program
(12-09-2015 09:01 PM)Marcus von Cube Wrote:  exact(5.25) in CAS returns 21/4.

Edit: Works in Home as well.

Thanks Marcus!.
it works perfectly (cas and Home).

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-09-2015, 11:55 PM
Post: #4
RE: Convert decimal > fraction in program
You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
12-10-2015, 10:18 AM (This post was last modified: 12-10-2015 10:19 AM by Dirk..)
Post: #5
RE: Convert decimal > fraction in program
A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.
Find all posts by this user
Quote this message in a reply
12-10-2015, 07:21 PM
Post: #6
RE: Convert decimal > fraction in program
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.
Thanks,
I need more help. See this program:
// Program to edit and show Matrix
EXPORT MeuSimp()
BEGIN
RECT();
EDITMAT(M1);
PRINT;
PRINT (exact(M1));
FREEZE;
END;
It works, but I want to see the matrix as in Figure 2
[Image: 7hHZY.jpg]
[Image: e4HIN.jpg]
How to?

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 02:40 PM (This post was last modified: 12-11-2015 03:57 PM by StephenG1CMZ.)
Post: #7
RE: Convert decimal > fraction in program
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

In a separate thread, Didier has pointed out that it is better to use STRING(Number,1) with numbers, as otherwise the string may lose precision. The modes can be combined by adding them, so I now suggest something like
MSGBOX(STRING(3.14,1+7)) or 1+14

As for printing a Matrix neatly, I do not know any simple solutions.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 04:00 PM
Post: #8
RE: Convert decimal > fraction in program
(12-11-2015 02:40 PM)StephenG1CMZ Wrote:  
(12-09-2015 11:55 PM)StephenG1CMZ Wrote:  You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.

In a separate thread, Didier has pointed out that it is better to use STRING(Number,1) with numbers, as otherwise the string may lose precision. The modes can be combined by adding them, so I now suggest something like
MSGBOX(STRING(3.14,1+7)) or 1+14
It is an interesting alternative StephenG1CMZ, but see that the goal was to show an matrix as in Figure 2, in a program that posted (MeuSimp). However, in both alternatives, does not appear to need.

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 04:10 PM
Post: #9
RE: Convert decimal > fraction in program
(12-10-2015 10:18 AM)Dirk. Wrote:  A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.

Displaying 5 for 0.5 certainly looks wrong - the Android emulator does this too.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 04:30 PM (This post was last modified: 12-11-2015 04:39 PM by jrozsas.)
Post: #10
RE: Convert decimal > fraction in program
The solution probably this in this command?
[Image: IXOzF.jpg]
I did it and got this result, but the program does not.
[Image: xAkJY.jpg]
The program with the command MSGBOX:
[Image: hlSp8.jpg]
and the program with PRINT [Image: jxBiH.jpg]

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 04:30 PM
Post: #11
RE: Convert decimal > fraction in program
(12-11-2015 04:10 PM)StephenG1CMZ Wrote:  
(12-10-2015 10:18 AM)Dirk. Wrote:  A bit off topic:

Is the (a+b/c)-Key working correctly? In scientific mode

0.5 (ENTER) (a+b/c)

shows 5 as a result. Shouldn't it show 1/2?

Pressing (a+b/c) again brings up 5E-1, which is correct.

Dirk.

Displaying 5 for 0.5 certainly looks wrong - the Android emulator does this too.

Yup. Already was on the list and I think corrected quite a while back. I'll check again though.

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
12-11-2015, 04:58 PM (This post was last modified: 12-11-2015 05:01 PM by luisphysics.)
Post: #12
RE: Convert decimal > fraction in program
It was not corrected. Just checked this on my physical calculator.When you type .5 it turns into 5E-1 and ab/c gives 5 rather than 1/2.
This does not work in Scientific or Engineering.

I want to make a note though. It works in CAS mode but not in the non-CAS.

-Luis-
Find all posts by this user
Quote this message in a reply
12-11-2015, 06:11 PM (This post was last modified: 12-11-2015 06:18 PM by jrozsas.)
Post: #13
RE: Convert decimal > fraction in program
I am close to the solution. See the result using "string" combined with "row".
// Program to edit and show Matrix
EXPORT MeuSimp()
BEGIN
LOCAL B,G;
RECT();
EDITMAT(M2);
B:=rowDim(M2);
PRINT;
FOR G FROM 1 TO B DO
PRINT(STRING(row(M2,G),7));
END;
FREEZE;
END;

[Image: bDqDG.jpg]
Suggestions?Must present the results in form of matrix and and fractions. See M2 (in decimal):
[Image: j7LGg.jpg]

Leo

Visit this user's website Find all posts by this user
Quote this message in a reply
12-11-2015, 06:57 PM (This post was last modified: 12-11-2015 07:00 PM by StephenG1CMZ.)
Post: #14
RE: Convert decimal > fraction in program
I was wondering whether what is needed is not exact(matrix), but matrix[1 2]:=exact(1.5) - making each individual element exact one by one. Unfortunately, I don't think that can be done - when I tried it I got a syntax error.

However in a spreadsheet entering exact(1.5) into a cell does work.
Although only the current cell is displayed formatted, the other cells being displayed decimal.
But once you have the exact elements in a rectangular set of spreadsheet cells (or maybe just a list or array), you could then print them out; but it will need formatting - I don't think that will be better than your Row solution.

Unless someone else has a solution...

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)