mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 10:22:12 +02:00
to <format>
: preserve round float numbers' type (#16016)
- fixes #16011 # Description `Display` implementation for `f64` omits the decimal part for round numbers, and by using it we did the same. This affected: - conversions to delimited formats: `csv`, `tsv` - textual formats: `html`, `md`, `text` - pretty printed `json` (`--raw` was unaffected) - how single float values are displayed in the REPL > [!TIP] > This PR fixes our existing json pretty printing implementation. > We can likely switch to using serde_json's impl using its PrettyFormatter which allows arbitrary indent strings. # User-Facing Changes - Round trips through `csv`, `tsv`, and `json` preserve the type of round floats. - It's always clear whether a number is an integer or a float in the REPL ```nushell 4 / 2 # => 2 # before: is this an int or a float? 4 / 2 # => 2.0 # after: clearly a float ``` # Tests + Formatting Adjusted tests for the new behavior. - 🟢 toolkit fmt - 🟢 toolkit clippy - 🟢 toolkit test - 🟢 toolkit test stdlib # After Submitting N/A --------- Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
/// A f64 wrapper that formats whole numbers with a decimal point.
|
||||
pub struct ObviousFloat(pub f64);
|
||||
|
||||
impl Display for ObviousFloat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let val = self.0;
|
||||
// This serialises these as 'nan', 'inf' and '-inf', respectively.
|
||||
if val.round() == val && val.is_finite() {
|
||||
write!(f, "{}.0", val)
|
||||
} else {
|
||||
write!(f, "{}", val)
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,7 +8,6 @@ mod range;
|
||||
#[cfg(test)]
|
||||
mod test_derive;
|
||||
|
||||
pub mod format;
|
||||
pub mod record;
|
||||
pub use custom_value::CustomValue;
|
||||
pub use duration::*;
|
||||
@@ -29,7 +28,7 @@ use chrono::{DateTime, Datelike, Duration, FixedOffset, Local, Locale, TimeZone}
|
||||
use chrono_humanize::HumanTime;
|
||||
use fancy_regex::Regex;
|
||||
use nu_utils::{
|
||||
SharedCow, contains_emoji,
|
||||
ObviousFloat, SharedCow, contains_emoji,
|
||||
locale::{LOCALE_OVERRIDE_ENV_VAR, get_system_locale_string},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -939,7 +938,7 @@ impl Value {
|
||||
match self {
|
||||
Value::Bool { val, .. } => val.to_string(),
|
||||
Value::Int { val, .. } => val.to_string(),
|
||||
Value::Float { val, .. } => val.to_string(),
|
||||
Value::Float { val, .. } => ObviousFloat(*val).to_string(),
|
||||
Value::Filesize { val, .. } => config.filesize.format(*val).to_string(),
|
||||
Value::Duration { val, .. } => format_duration(*val),
|
||||
Value::Date { val, .. } => match &config.datetime_format.normal {
|
||||
|
@@ -307,10 +307,8 @@ mod int_range {
|
||||
}
|
||||
|
||||
mod float_range {
|
||||
use crate::{
|
||||
IntRange, Range, ShellError, Signals, Span, Value, ast::RangeInclusion,
|
||||
format::ObviousFloat,
|
||||
};
|
||||
use crate::{IntRange, Range, ShellError, Signals, Span, Value, ast::RangeInclusion};
|
||||
use nu_utils::ObviousFloat;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{cmp::Ordering, fmt::Display, ops::Bound};
|
||||
|
||||
|
Reference in New Issue
Block a user