Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationalizing Numerical Output

Consider :

Grid@Partition[
     Text[Style[ToString[Range[0, 180, 22.5][[#]]] <> "\[Degree]", Bold, 16,
          GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]

enter image description here

How can I get rid of the dot following the Integers ?

like image 901
500 Avatar asked Sep 17 '11 01:09

500


1 Answers

If a number becomes an integer when you rationalize it, use the integer; otherwise stick with the original number. This is achieved by a simple function, f[x]:

f[x_] := If[IntegerQ[n = Rationalize[x]], n, x]

Testing...

f[67.5]
f[0.]
f[45.]

(* Out  *)
67.5
0
45

You can't just Rationalize all the values, as the following makes clear:

rationalize

To see how it works in your case, just insert (f/@) into your code to reformat the values output from Range:

Grid@Partition[
Text[Style[
  ToString[(f/@ Range[0, 180, 22.5])[[#]]] <> "\[Degree]", 
  Bold, 16, GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]

So

temps

like image 156
DavidC Avatar answered Dec 03 '22 02:12

DavidC