mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:15:49 +02:00
@ -1099,8 +1099,18 @@ impl Expression {
|
||||
Expression::Literal(Literal::Number(Number::Int(BigInt::from(i))))
|
||||
}
|
||||
|
||||
pub fn decimal(f: f64) -> Expression {
|
||||
Expression::Literal(Literal::Number(Number::Decimal(BigDecimal::from(f))))
|
||||
pub fn decimal(f: f64) -> Result<Expression, ParseError> {
|
||||
let dec = BigDecimal::from_f64(f);
|
||||
|
||||
let dec = match dec {
|
||||
Some(x) => Ok(x),
|
||||
None => Err(ParseError::internal_error(
|
||||
"Can not convert f64 to big decimal"
|
||||
.to_string()
|
||||
.spanned_unknown(),
|
||||
)),
|
||||
}?;
|
||||
Ok(Expression::Literal(Literal::Number(Number::Decimal(dec))))
|
||||
}
|
||||
|
||||
pub fn string(s: String) -> Expression {
|
||||
|
@ -17,6 +17,7 @@ use crate::value::primitive::Primitive;
|
||||
use crate::value::range::{Range, RangeInclusion};
|
||||
use crate::{ColumnPath, PathMember, UnspannedPathMember};
|
||||
use bigdecimal::BigDecimal;
|
||||
use bigdecimal::FromPrimitive;
|
||||
use chrono::{DateTime, Utc};
|
||||
use indexmap::IndexMap;
|
||||
use itertools::Itertools;
|
||||
@ -190,8 +191,22 @@ impl UntaggedValue {
|
||||
}
|
||||
|
||||
/// Helper for creating decimal values
|
||||
pub fn decimal(s: impl Into<BigDecimal>) -> UntaggedValue {
|
||||
UntaggedValue::Primitive(Primitive::Decimal(s.into()))
|
||||
pub fn decimal(s: BigDecimal) -> UntaggedValue {
|
||||
UntaggedValue::Primitive(Primitive::Decimal(s))
|
||||
}
|
||||
|
||||
/// Helper for creating decimal values
|
||||
pub fn decimal_from_float(f: f64, span: Span) -> UntaggedValue {
|
||||
let dec = BigDecimal::from_f64(f);
|
||||
|
||||
match dec {
|
||||
Some(dec) => UntaggedValue::Primitive(Primitive::Decimal(dec)),
|
||||
None => UntaggedValue::Error(ShellError::labeled_error(
|
||||
"Can not convert f64 to big decimal",
|
||||
"can not create decimal",
|
||||
span,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper for creating binary (non-text) buffer values
|
||||
|
Reference in New Issue
Block a user