如何在Racket中更改CSV写入的输出精度?

huangapple go评论53阅读模式
英文:

How do I alter the precision of output from csv-writing in Racket?

问题

我正在使用csv-writing包中的display-table,并希望在将数字转换为输出字符串时更改默认精度。我可以看到文档上说要使用make-csv-printing-params和*#:number-cell->string*,但它还说要使用~r参数*#:precision*。我尝试让所有这些都配合在一起,但没有成功:

(display-table each_rank_table vars_out_port
               #:printing-params
               (make-csv-printing-params
               #:number-cell->string
               (~r #:precision 8)))

我假设我需要以某种方式将单元格值传递给~r(?),所以我认为我没有以正确的方式进行操作。

英文:

I am using display-table from the csv-writing package, and would like to alter the default precision when converting numbers to strings for output. I can see the docs say to use make-csv-printing-params and #:number-cell->string, but it also says it uses the ~r parameter #:precision. I tried to make all this fit together, but not successfully:

(display-table each_rank_table vars_out_port
           #:printing-params
           (make-csv-printing-params
           #:number-cell->string
           (~r #:precision 8)))

I assume I somehow need to get the cell value into ~r (?), so I presume I'm not going about this in the right way.

答案1

得分: 2

你需要提供一个接受数值参数并返回字符串的函数 - 最简单的方法是只调用~r

示例:

(make-csv-printing-params
  #:number-cell->string (curry ~r #:precision 8))

或者

(make-csv-printing-params
  #:number-cell->string (lambda (n) (~r n #:precision 8)))

请注意,如果你希望输出中包含尾随的0,你需要使用'(= 8)作为精度参数:

> (~r 3.2 #:precision 8)
"3.2"
> (~r 3.2 #:precision '(= 8))
"3.20000000"
英文:

You have to give it a function that takes a numeric argument and returns a string - one that just calls ~r would be easiest.

Examples:

(make-csv-printing-params
  #:number-cell->string (curry ~r #:precision 8))

or

(make-csv-printing-params
  #:number-cell->string (lambda (n) (~r n #:precision 8)))

Note that if you want trailing 0's in the output, you need to use '(= 8) for the precision:

> (~r 3.2 #:precision 8)
"3.2"
> (~r 3.2 #:precision '(= 8))
"3.20000000"

huangapple
  • 本文由 发表于 2023年7月18日 05:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定