Decimals in text.

goldenj shared this question 2 months ago
Answered

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!

Best Answer
photo

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

Comments (18)

photo
1

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

.

photo
1

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

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

photo
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)

photo
1

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

No. you can use the rounding you want.

saludos

photo
1

@mathmagic

In my environment GGB 5.0.366 Win10

I must set Menu/option/rounding to 4

Files: test.ggb
photo
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?

photo
1

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

photo
1

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

my question continues without answer

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

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

photo
1

this is better:

.

text1 = "$ "

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

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

+ "."

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

.

photo
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

photo
1

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

photo
photo
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]]])]

photo
1

Whoa!

photo
photo
1

A late entry to the contest :)

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

photo
1

Great !!

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

photo
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
  • Contact us
    • Feedback & Questions
    • This email address is being protected from spambots. You need JavaScript enabled to view it.
    • +43 677 6137 2693
© 2017 International GeoGebra Institute