let s = Printf.sprintf "%d" 1;;
let _ = print_endline s;;
работает.
let s = Printf.sprintf ("%" ^ "d") 1;;
let _ = print_endline s;;
ocamlc ./test.ml
File "./test.ml", line 1, characters 23-34:
This expression has type string but is here used with type
('a -> 'b, unit, string) format =
('a -> 'b, unit, string, string, string, string) format6
Здравствуйте, _rasta, Вы писали:
_>а если мне надо получить что-то вроде "%20s" ?
Это невозможно. Да и зачем? Если надо выбирать точность представления чисел, то вот:
The integer in a width or precision can also be specified as *, in which case an extra integer argument is taken to specify the corresponding width or precision. This integer argument precedes immediately the argument to print, unless an optional positional specifier is given to indicates which argument to use. For instance, %.*3$f prints a float with as many fractional digits as the value of the third argument.
Vintik_69 wrote:
> _>нет, я хочу получить строку длинной в n символов, причем n я вычисляю. > _>однако жаль... > > А поподробнее?
подробнее...?
ну что-т вроде:
(** вот примерно так хочется видеть получение строки *)
(**
let s =
let templ = "%" ^ Sys.argv.(1) ^ "s" in
Printf.sprintf templ Sys.argv.(2);;
*)
(** вот так вот сейчас сделано *)let spaces =
(String.make
((int_of_string Sys.argv.(1)) - String.length Sys.argv.(2)) ' ')
;;
(** «|» добавил только для того, чтобы наглядно было видно результат :) *)let s = "|" ^ spaces ^ Sys.argv.(2) ^ "|";
;;
let _ = print_endline s
;;
_>(** вот примерно так хочется видеть получение строки *)
_>(**
_> let s =
_> let templ = "%" ^ Sys.argv.(1) ^ "s" in
_> Printf.sprintf templ Sys.argv.(2);;
_>*)
_>