# Decimals in text.

goldenj shared this question 5 months ago

I'm doing an activity for money, so I'd like to force two decimals even if it's an integer. I've tried in the text box settings and the whole applet. I've tried adding .001 to the variable as well.

Ideas or insights? Thanks for any help!

perhaps

text1 = If[IsDefined[IndexOf[".", "\$"b]], Take["\$" b "0", 1, IndexOf[".", "\$"b] + 2], "\$" b ".00"]

or larger

text2 = If[IsDefined[IndexOf[".", "\$" round(b, 2)]], Take["\$" round(b, 2) "0", 1, IndexOf[".", "\$" round(b, 2)] + 2], "\$" round(b, 2) ".00"]

I tested it without problem

1

1. text1 = "\$" + (money + 0.001) + "" <- properties/text/rounding == 3
2. text2 = First[text1, Length[text1] - 1]

.

1

Take["\$" (num + 0.0001), 1, IndexOf[".", "\$" (num + 0.0001)] + 2]

so you can use any rounding option for another numbers (ie: interest)

1

In my first solution you must set rounding local for text1.

In the solution of mathmagic you must set rounding global in Menu/Option.

.

Here a solution is independent from rounding-options (and also OK for num < 0)

.

"\$ "

+ (If[sgn(num) < 0, "-", ""])

+ (First["" + (abs(round(num 100))), Length["" + (abs(round(num 100)))] - 2])

+ "." + (Last["" + (abs(round(num 100))), 2])

.

(above in one line)

1

1. In the solution of mathmagic you must set rounding global in Menu/Option.

No. you can use the rounding you want.

saludos

1

@mathmagic

In my environment GGB 5.0.366 Win10

I must set Menu/option/rounding to 4

Files: test.ggb
1

Ok

it is possible I have 5 decimals by default.

a question: If I select for a text 5 decimals in local properties of the text, how I return the text to global selection?

1

It seems the local rounding (properties/text/rounding) has no effect for this command. Maybe this is a a bug.

1

the effect is lost for indexof[] command, perhaps the decimal affects only at final showed text

1

Silly that I didn't think of text commands.

Now I'm going crazy! Rami's solution worked perfectly, except when num=0 it gave me ?.?. So I added a textbox to show \$0.00 when money = 0 and now the same text that worked before only gives ?.? Later: now it's working again, except for the 0 case. I don't know what changed!

Here's the current: https://www.geogebra.org/m/bNwhCNFb

And I'll attach one with both versions of the solution. ¡Mil gracias, mi GGB gente! I was being grateful for your help, and my spouse asked if either of you were on Twitter or in the #mtbos. Are you? (I'm @mathhombre there.)

1

I hope this version works in all cases,

exept infinity (example: 0/1)

.

.text1 = "\$ "

+ (If[sgn(money) < 0, "-", ""])

+ (First["" + (If[abs(round(money 100)) < 100, "0", ""]) + (abs(round(money 100))), Max[1, Length["" + (abs(round(money 100)))] - 2]])

+ "."

+ (Last["00" + (abs(round(money 100))), 2])

.

all in one line

For some applications the second expression (sign) is optional

.

I hope somebody else create a version more short/clear in one line and independent of rounding parameter.

1

this is better:

.

text1 = "\$ "

+ (If[sgn(money) < 0, "-", ""])

+ ("" + (floor(abs(round(money 100) / 100))))

+ "."

+ (Last["00" + (abs(round(money 100))), 2])

.

1

perhaps

text1 = If[IsDefined[IndexOf[".", "\$"b]], Take["\$" b "0", 1, IndexOf[".", "\$"b] + 2], "\$" b ".00"]

or larger

text2 = If[IsDefined[IndexOf[".", "\$" round(b, 2)]], Take["\$" round(b, 2) "0", 1, IndexOf[".", "\$" round(b, 2)] + 2], "\$" round(b, 2) ".00"]

I tested it without problem

1

Fascinating! Very slick use of IsDefined with the text version of a number.

1

a=123.4567

If[a ≟ 0, "0." + (Sum[Sequence["0", i, 1, 2]]), If[a < 0, "-",""] + (UnicodeToText[Reverse[Insert[TextToUnicode["."], Reverse[TextToUnicode[If[Length["" round(abs(a) 10², 0)] ≤ 2, Sum[Sequence["0", i, 1, abs(2 - Length["" round(abs(a) 10², 0)] )+ 1] ] ,""]+ "" round(abs(a) 10², 0)]], 2+1]]])]

1

Whoa!

1

A late entry to the contest :)

1. a=123.4567
2. TableText[{{a}},"a"]

1

Great !!

Additional comment: need in Properties/Text/Rounding = 2 (or something else)

1

suppose you have a tabletext in algebra view. then you drag and drop the text from algebra view to graphics

a new text is created

is this the desirable behavior?

• GeoGebra
• Help
• Partners
• Feedback & Questions
• This email address is being protected from spambots. You need JavaScript enabled to view it.
• +43 677 6137 2693