Wolfram: Convert to String

By Xah Lee. Date: . Last updated: .

ToString

ToString[expr,form]

return a string, formatted using form.

💡 TIP: use InputForm as second argument. That is the most useful.

ToString[x^2, InputForm] === "x^2"
(* True *)

(* number to string *)
ToString[3, InputForm] === "3"
(* True *)

(* number to string *)
ToString[123.456, InputForm] === "123.456"
(* True *)

(* fraction *)
ToString[2/3, InputForm]
(* 2/3 *)

(* convert fraction to machine number first *)
ToString[N[2/3], InputForm]
(* 0.6666666666666666 *)

(* convert symbolic constant to number first *)
ToString[N[Pi] , InputForm]
(* 3.141592653589793 *)
ToString[expr]

Convert number or any expression to string, formatted using OutputForm. Basically, 2-dimensional ASCII text for human to read.

ToString[ 8.23 ] === "8.23"
(* True *)

ToString[ {1,2,3} ] === "{1, 2, 3}"
(* True *)

ToString[x^2] ===
" 2
x"
(* True *)

ToString[x^2/y^3] ===
" 2
x
--
 3
y"
(* True *)

ToString[ Expand[(x^2/y^3 + y^2)^2] ] ===
" 4      2
x    2 x     4
-- + ---- + y
 6    y
y"

Convert Number to String, Controlling Number of Digits

NumberForm
  • NumberForm[number, n]
  • NumberForm[number, {n,f}]

show total of n digits, with f in decimal places.

  • Show total of n digits
  • If total digits is smaller than the number of digits in integer part, warning is shown

most useful options:

  • NumberPadding -> {"" , "0"}
  • NumberPoint -> "."
  • NumberSeparator -> {",",""}
  • NumberSigns -> {"-", ""}
  • ScientificNotationThreshold -> {-5, 6}
(* convert number to string, controlling total number of digits *)

ToString[NumberForm[123.456789, 3] ]
(* 123. *)

ToString[NumberForm[123.456789, 4] ]
(* 123.5 *)
(* if total digits is smaller than the number of digits in integer part, warning is shown *)
ToString[NumberForm[123.456789, 2], InputForm]
(* NumberForm::reqsigz: Requested number precision is lower than number of digits shown; padding with zeros. *)
(* 120. *)
(* convert number to string. *)

ToString[NumberForm[123.456789, {4,2}] ]
(* 123.50 *)

ToString[NumberForm[123.456789, {4,5}] ]
(* 123.50000 *)

(*  total of 99 digits precision, with 2 decimal places *)
ToString[NumberForm[123.456789, {99,2}] ]
(* 123.46 *)

TextString (Convert to Human Readable Format)

TextString is designed to convert arbitrary expression or data into a human readable format (as string), including arbitrary data object such as date, geographical data, missing data, etc, and auto omitting parts of a long list by triple dots, and customize list brackets and delimiters, etc.

TextString[Now]
(* Thu 4 Sep 2025 15:22:33 *)

ToString[Now]
(* DateObject[{2025, 9, 4, 15, 22, 42.1365622}, Instant, Gregorian, -7.] *)
Wolfram TextString 2025-09-04 2544d
Wolfram TextString 2025-09-04 2544d

Wolfram. Number

Wolfram. String