knitr:保留尾随零以获得一致数量的有效数字/数字

问题描述 投票:0回答:1

使用

knitr
/
bookdown
(如 1.0e-9not 1e-9)生成 PDF 输出时是否可以保留数字中的尾随零,以确保有效数字的数量一致所有打印的数字?

最小示例:

---
title: "Test"
author: "Author"
output: pdf_document
---

`r options(scipen=3, digits=2)`

# Introduction

I would like to have the number $`r signif(1.0e-9, 2)`$ to preserve the format 
including the trailing zero to match the length of $`r signif(1.1e-9, 2)`$.

Using  $`r sprintf(signif(1.0e-9, 2), fmt='%#.2g')`$ doesn't seem to be an option either.

已经有一个关于 Knit 选项的相关问题:包括尾随零的确切位数(以及打印表格或数字时关于同一问题的其他几个问题),但令我惊讶的是,它们都与我的不完全相同问题。

formatdown
包可能有效,但我更喜欢一个没有额外依赖项的简单解决方案。

r latex knitr pandoc bookdown
1个回答
0
投票

我建议

sprintf
效果很好:

signif(1e-9, 2)
# [1] 1e-09
signif(1.1e-9, 2)
# [1] 1.1e-09

sprintf("%0.01e", c(1e-9, 1.1e-9))
# [1] "1.0e-09" "1.1e-09"
© www.soinside.com 2019 - 2024. All rights reserved.