MCPcopy Create free account
hub / github.com/apache/datafusion / format_float

Method format_float

datafusion/spark/src/function/string/format_string.rs:1746–1892  ·  view source on GitHub ↗
(&self, writer: &mut String, value: f64)

Source from the content-addressed store, hash-verified

1744 }
1745
1746 fn format_float(&self, writer: &mut String, value: f64) -> Result<()> {
1747 self.validate_grouping_separator()?;
1748
1749 let mut prefix = String::new();
1750 let mut suffix = String::new();
1751 let mut number = String::new();
1752 let upper = self.conversion_type.is_upper();
1753
1754 // set up the sign
1755 if value.is_sign_negative() {
1756 if self.negative_in_parentheses {
1757 prefix.push('(');
1758 suffix.push(')');
1759 } else {
1760 prefix.push('-');
1761 }
1762 } else if self.space_sign {
1763 prefix.push(' ');
1764 } else if self.force_sign {
1765 prefix.push('+');
1766 }
1767
1768 if value.is_finite() {
1769 let mut use_scientific = false;
1770 let mut strip_trailing_0s = false;
1771 let mut abs = value.abs();
1772 let mut exponent = abs.log10().floor() as i32;
1773 let mut precision = match self.precision {
1774 NumericParam::Literal(p) => p,
1775 _ => 6,
1776 };
1777 match self.conversion_type {
1778 ConversionType::DecFloatLower => {
1779 // default
1780 }
1781 ConversionType::SciFloatLower => {
1782 use_scientific = true;
1783 }
1784 ConversionType::SciFloatUpper => {
1785 use_scientific = true;
1786 }
1787 ConversionType::CompactFloatLower | ConversionType::CompactFloatUpper => {
1788 strip_trailing_0s = true;
1789 if precision == 0 {
1790 precision = 1;
1791 }
1792 // exponent signifies significant digits - we must round now
1793 // to (re)calculate the exponent
1794 let rounding_factor =
1795 10.0_f64.powf((precision - 1 - exponent) as f64);
1796 let rounded_fixed = (abs * rounding_factor).round();
1797 abs = rounded_fixed / rounding_factor;
1798 exponent = abs.log10().floor() as i32;
1799 if exponent < -4 || exponent >= precision {
1800 use_scientific = true;
1801 precision -= 1;
1802 } else {
1803 // precision specifies the number of significant digits

Callers 1

formatMethod · 0.80

Calls 9

newFunction · 0.85
trim_trailing_0sFunction · 0.85
is_upperMethod · 0.80
is_infiniteMethod · 0.80
pushMethod · 0.45
containsMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected