Consider :
Grid@Partition[
Text[Style[ToStri开发者_运维问答ng[Range[0, 180, 22.5][[#]]] <> "\[Degree]", Bold, 16,
GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]
How can I get rid of the dot following the Integers ?
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:
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
Although the original question doesn't have any numbers with exponents, it would be safest in general to use NumberForm as follows:
trimPoint[n_] :=
NumberForm[n,
NumberFormat -> (DisplayForm@
RowBox[Join[{StringTrim[#1, RegularExpression["\\.$"]]},
If[#3 != "", {
"\[Times]", SuperscriptBox[#2, #3]}, {}]]
] &)]
Then you only have to modify the original code by inserting //trimPoint as follows:
Grid@Partition[
Text[Style[
ToString[Range[0, 180, 22.5][[#]] // trimPoint] <> "\[Degree]",
Bold, 16, GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]
Another possibility is not to generate them in the first place.
If[IntegerQ[#], #, N@#] & /@ Range[0, 180, 45/2]
giving
{0, 22.5, 45, 67.5, 90, 112.5, 135, 157.5, 180}
In general, you should use Rationalize
.
Rationalize@10.
Out[1] = 10
However in your case, you shouldn't simply use Rationalize
, as you don't want to operate on some of the elements. Here's a simple approach that will do what you want.
list = Range[0, 180, 22.5] /. (x_ /; FractionalPart@x == 0.) ->
IntegerPart@x
Grid@Partition[
Text[Style[ToString[list[[#]]] <> "\[Degree]", Bold, 16,
GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]
The code above generates the same list as yours, and then conditionally replaces those elements which have a FractionalPart
equal to 0.
(e.g., 10.
), with its IntegerPart
(e.g. 10
).
Another option is to remove any trailing "."
using StringTrim
:
Grid@Partition[
Text[Style[
StringTrim[ToString[Range[0, 180, 22.5][[#]]], "."] <> "\[Degree]",
Bold, 16, GrayLevel[(8 - #)/10]]] & /@ Range[8], 2, 1]
精彩评论