2
$\begingroup$

I have a Style displaying an underlying Real value. For example I might have

Style[NumberForm[10.01, {10, 2}], FontFamily -> "Academy Engraved LET", FontSize -> 50, FontColor -> RGBColor[0, 1, 0]]

or

Style[1201/100000, FontFamily -> "Charter", FontSize -> 20]

or potentially any other arbitrary Style expression representing a Real.

and I want to get the values 10.01 and 0.01201, respectively.

$\endgroup$
  • $\begingroup$ It's probably best to try to avoid storing values together with wrappers like Style, MatrixForm, NumberForm, etc. Apply the wrappers only before display, but not while doing computations. We need to do the same when using Dynamic. Of course this is not an answer, just a suggestion. $\endgroup$ – Szabolcs Nov 13 '14 at 19:08
  • $\begingroup$ @Szabolcs: Good advice though. I worked myself into a corner. $\endgroup$ – orome Nov 13 '14 at 19:26
  • 3
    $\begingroup$ f[x_?NumericQ] := N @ x; f[x_] := f @ First[x] ? $\endgroup$ – Kuba Nov 13 '14 at 19:57
  • $\begingroup$ @Kuba: Excellent! I tried just about every guess close to that, but never quite hit on the right formulation! $\endgroup$ – orome Nov 13 '14 at 20:08
3
$\begingroup$
stripF = ToExpression[ToString[#, StandardForm]] &;

stripF /@ {Style[1201/100000, FontFamily -> "Charter",  FontSize -> 20], 
  Style[NumberForm[10.01, {10, 2}], FontFamily -> "Academy Engraved LET",
        FontSize -> 50, FontColor -> RGBColor[0, 1, 0]]}
(* {1201/100000, 10.01} *)

Also:

CalculateUtilities`StringUtilities`Private`stripformatting /@ 
 {Style[1201/100000, FontFamily -> "Charter", FontSize -> 20], 
  Style[NumberForm[10.01, {10, 2}], FontFamily -> "Academy Engraved LET",
        FontSize -> 50, FontColor -> RGBColor[0, 1, 0]]}
(* {1201/100000, 10.010} *)
$\endgroup$
2
$\begingroup$

You may never know how many wrappers there are but those functions have this in common that first argument is what we only care about.

f[x_?NumericQ] := N @ x;  
f[x_] := f @ First[x]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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