Seite 1 von 1
(gelöst) Ausgabe einer Zahl als Bruch
Verfasst: Mi, 11.08.2021 19:17
von clag
Hallo Kenner und Könner
ich möchte gern eine im Makro errechnete Zahl als Bruch darstellen bzw in eine Text einfügen
für eine Zelle gibt es ja das Format Bruch, aber die Basic Funktion "Format" kann das wohl nicht !?
Wie generiert man ein Bruch Format für die Ausgabe ?
Danke fürs lesen und ggf. Lösungsansätze
Re: Ausgabe einer Zahl als Bruch
Verfasst: Mi, 11.08.2021 20:03
von Stephan
das hier fügt einen Bruch a/b am Ende des Textes eines Textdokuments ein:
Code: Alles auswählen
Sub InsertFormula()
oShape = ThisComponent.createInstance("com.sun.star.text.TextEmbeddedObject")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
dim size as new com.sun.star.awt.Size
size.Height = 2000
size.Width = 1000
oShape.setSize(size)
T = ThisComponent.Text
T.insertTextContent(T.End, oShape, False)
oShape.Model.Formula = "{a} over {b}"
End Sub
basierend auf:
https://forum.openoffice.org/en/forum/v ... iter+shape
GRuß
Stephan
Re: Ausgabe einer Zahl als Bruch
Verfasst: Mi, 11.08.2021 21:01
von mikeleb
Hallo,
liefert bei mir die korrekte Ausgabe 1/4. Insofern kann Format() mit dem Bruchformat umgehen. Allerdings kommt man damit sehr schnell an Grenzen.
0,05 liefert 0
0,058 liefert 1/9
Da wird offensichtlich sehr stark gerundet.
Re: Ausgabe einer Zahl als Bruch
Verfasst: Mi, 11.08.2021 22:02
von Stephan
z.B. hier ist ein fertiger VBA-Code:
http://www.arstechnica.de/index.html?na ... a0033.html
in MS Excel läuft der prima, aber in OO/LO liefert er FEhler, also muss man ihn etwas anpassen und der Einfachheithalber ein "On Error Resume Next" ergänzen:
Code: Alles auswählen
Sub start
Msgbox DezimalZahlnBruch(0.058)
End Sub
Function DezimalZahlnBruch(DezimalZahl As Double)
Dim A, Approx As Double
Dim Z0 As Long, Z1 As Long, Z2 As Long
Dim N0 As Long, N1 As Long, N2 As Long
Dim X0 As Double, X1 As Double, X2 As Double
On Error Resume Next
A = Int(DezimalZahl)
Z0 = 0: Z1 = 1
N0 = 1: N1 = 0
X0 = 1: X1 = (DezimalZahl - A)
Do
Z2 = A * Z1 + Z0: Z0 = Z1: Z1 = Z2
N2 = A * N1 + N0: N0 = N1: N1 = N2
A = Int(X0 / X1)
X2 = X0 - A * X1: X0 = X1: X1 = X2
Approx = Z2 / N2
Loop Until (N2 = 0 Or Approx = DezimalZahl)
DezimalZahlnBruch = "Bruch ist: " & Z2 & "/" & N2
End Function
Gruß
Stephan
Re: Ausgabe einer Zahl als Bruch
Verfasst: Do, 12.08.2021 22:39
von clag
Hallo allerbesten Dank
die von @mikeleb vorgeschlagene Variante über format is in der Tat recht grob
der von Stephan vorgeschlagene VBA code ist genau so etwas wie ich mir das vorgestellt hatte
habe es noch etwas für mich angepasst siehe die letzten 7 Zeilen
Code: Alles auswählen
Function DezimalZahlnBruch(DezimalZahl As Double)
Dim A, Approx As Double
Dim Z0 As Long, Z1 As Long, Z2 As Long
Dim N0 As Long, N1 As Long, N2 As Long
Dim X0 As Double, X1 As Double, X2 As Double
On Error Resume Next
A = Int(DezimalZahl)
Z0 = 0: Z1 = 1
N0 = 1: N1 = 0
X0 = 1: X1 = (DezimalZahl - A)
Do
Z2 = A * Z1 + Z0: Z0 = Z1: Z1 = Z2
N2 = A * N1 + N0: N0 = N1: N1 = N2
A = Int(X0 / X1)
X2 = X0 - A * X1: X0 = X1: X1 = X2
Approx = Z2 / N2
Loop Until (N2 = 0 Or Approx = DezimalZahl)
XX = fix (Z2/N2)
YY = Z2 - XX*N2
if XX=0 then
DezimalZahlnBruch = YY & "/" & N2
else
DezimalZahlnBruch = XX & " " & YY & "/" & N2
end if
End Function
nochmals meinen Dank