0
$\begingroup$

My question essentially asks everything but I shall rephrase it to make certain it is well understood.

1. How to print all entries in rows of a column so that they align perfectly at the decimal point.

2. How to make 2-digit floating point precision for cents values (i.e. $1.98)?

3. If this cannot be done with TableForm can you suggest another option?

Here is my Mathematica code...

carValue := 
 RecurrenceTable[{a[i] == a[i - 1] * 0.90, a[1] == 30000 , 
   y[i] == i}, {y, a}, {i, 15}]
TableForm[carValue , TableHeadings -> {None, {"Year", "Car Price"}}]
Labeled[Select[carValue, #[[2]] < 8000 &, 1][[1, 
  1]], "Year when valued less than $8000:", Left]
$\endgroup$

2 Answers 2

1
$\begingroup$
carValue := 
 RecurrenceTable[{
    a[i] == a[i - 1]*0.7
    , a[1] == 30000
    , y[i] == i}
    , {y,a}, {i, 15}]

Grid[
    Join[
       {{"Year", Item["Car Price", Alignment -> Right]}}
      ,Round[carValue, 0.01]
      ]
    , Alignment -> "."
    , Dividers -> {None, {False,True, False}}]

enter image description here

Notes :

TableForm has probably no way to align on the decimal point.

With Tableform, a workaround could be to use NumberForm[#, {10,2}] & to have equally formatted numbers, but it gives :

enter image description here

I don't like the 00 after the decimal point. Furthermore it' s not in the philosophy of your question.

That's the reason why I have used Grid.

But Grid doesnt' have the option TableHeading

This is not a problem, it suffices to add a row with the information. (Grid is very robust. It accepts rows of different length, which is very usefull)

Round is used to have only to 2 digits after the decimal point.

$\endgroup$
1
  • $\begingroup$ thank you so much. your grid solution works just fine. $\endgroup$ Commented May 1, 2020 at 12:08
1
$\begingroup$

Another option might be

carValue = RecurrenceTable[{a[i] == a[i - 1]*0.90, a[1] == 30000, y[i] == i}, {y, a}, {i, 15}];

carValueFixed = {First[#], 
     PaddedForm[Last[#], {7, 2}, DigitBlock -> 3, 
      NumberSeparator -> ",", NumberPadding -> {" ", "0"}]} & /@ carValue;

TableForm[carValueFixed, TableHeadings -> {None, {"Year", "Car Price"}}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ thank you. another fine solution. $\endgroup$ Commented May 1, 2020 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.