2
$\begingroup$

I would like to print the number

0.21534715 

as

.21

NumberForm[0.21534715, {1, 2}]

gives me only

0.20
$\endgroup$
  • 2
    $\begingroup$ StringTrim[ToString@.21, StartOfString ~~ ("0" ..)]? $\endgroup$ – Kuba Feb 18 at 9:14
  • $\begingroup$ Great, but I do not understand StartOfString ~~ ("0" ..). Can you write instead a longer expression? Do you have also solution without converting to String? $\endgroup$ – lio Feb 18 at 9:29
  • $\begingroup$ StartOfString "represents the start of a string for purposes of matching in StringExpression"; ~~ is StringExpression, and .. is Repeated. These are used in pattern matching and regular expressions. $\endgroup$ – corey979 Feb 18 at 9:43
  • $\begingroup$ You can select each part and hit F1, I am not sure how to help. $\endgroup$ – Kuba Feb 18 at 9:43
2
$\begingroup$
nForm = NumberForm[#, 
   NumberFormat -> (Row[{If[StringStartsQ["0"]@#, StringDrop[#, 1], #], #3}]&)]&;

lst = {000.0123, 123.^7, 00013.123};
nForm[lst]

{.0123,4.2592814,13.123}

$\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.