mirror of
https://github.com/nushell/nushell
synced 2024-12-26 04:53:09 +00:00
Fmt f64 (#9142)
Fixes: #9131 As octal and some other format not valid for f64 we have to specify it . just wonder if need for generic impl or no for just one type ?
This commit is contained in:
parent
dacf80f34a
commit
6a0c88d516
1 changed files with 36 additions and 1 deletions
|
@ -82,13 +82,14 @@ fn fmt(
|
|||
|
||||
fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
match input {
|
||||
Value::Float { val, .. } => fmt_it_64(*val, span),
|
||||
Value::Int { val, .. } => fmt_it(*val, span),
|
||||
Value::Filesize { val, .. } => fmt_it(*val, span),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { .. } => input.clone(),
|
||||
other => Value::Error {
|
||||
error: Box::new(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "integer or filesize".into(),
|
||||
exp_input_type: "float , integer or filesize".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: span,
|
||||
src_span: other.expect_span(),
|
||||
|
@ -131,6 +132,40 @@ fn fmt_it(num: i64, span: Span) -> Value {
|
|||
Value::Record { cols, vals, span }
|
||||
}
|
||||
|
||||
fn fmt_it_64(num: f64, span: Span) -> Value {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("binary".into());
|
||||
vals.push(Value::string(format!("{:b}", num.to_bits()), span));
|
||||
|
||||
cols.push("debug".into());
|
||||
vals.push(Value::string(format!("{num:#?}"), span));
|
||||
|
||||
cols.push("display".into());
|
||||
vals.push(Value::string(format!("{num}"), span));
|
||||
|
||||
cols.push("lowerexp".into());
|
||||
vals.push(Value::string(format!("{num:#e}"), span));
|
||||
|
||||
cols.push("lowerhex".into());
|
||||
vals.push(Value::string(format!("{:0x}", num.to_bits()), span));
|
||||
|
||||
cols.push("octal".into());
|
||||
vals.push(Value::string(format!("{:0o}", num.to_bits()), span));
|
||||
|
||||
// cols.push("pointer".into());
|
||||
// vals.push(Value::string(format!("{:#p}", &num), span));
|
||||
|
||||
cols.push("upperexp".into());
|
||||
vals.push(Value::string(format!("{num:#E}"), span));
|
||||
|
||||
cols.push("upperhex".into());
|
||||
vals.push(Value::string(format!("{:0X}", num.to_bits()), span));
|
||||
|
||||
Value::Record { cols, vals, span }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in a new issue