HP Forums
Convert decimal > fraction in program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Convert decimal > fraction in program (/thread-5303.html)



Convert decimal > fraction in program - jrozsas - 12-09-2015 08:23 PM

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?


RE: Convert decimal > fraction in program - Marcus von Cube - 12-09-2015 09:01 PM

exact(5.25) in CAS returns 21/4.

Edit: Works in Home as well.


RE: Convert decimal > fraction in program - jrozsas - 12-09-2015 10:56 PM

(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).


RE: Convert decimal > fraction in program - StephenG1CMZ - 12-09-2015 11:55 PM

You can also use STRING(NUMBER,MODE) where a mode of 7 or 14, for example, will give a string having a fraction format.


RE: Convert decimal > fraction in program - Dirk. - 12-10-2015 10:18 AM

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.


RE: Convert decimal > fraction in program - jrozsas - 12-10-2015 07:21 PM

(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?


RE: Convert decimal > fraction in program - StephenG1CMZ - 12-11-2015 02:40 PM

(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.


RE: Convert decimal > fraction in program - jrozsas - 12-11-2015 04:00 PM

(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.


RE: Convert decimal > fraction in program - StephenG1CMZ - 12-11-2015 04:10 PM

(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.


RE: Convert decimal > fraction in program - jrozsas - 12-11-2015 04:30 PM

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]


RE: Convert decimal > fraction in program - Tim Wessman - 12-11-2015 04:30 PM

(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.


RE: Convert decimal > fraction in program - luisphysics - 12-11-2015 04:58 PM

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.


RE: Convert decimal > fraction in program - jrozsas - 12-11-2015 06:11 PM

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]


RE: Convert decimal > fraction in program - StephenG1CMZ - 12-11-2015 06:57 PM

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...