MCPcopy Index your code
hub / github.com/RustPython/RustPython / format_float

Method format_float

crates/common/src/cformat.rs:495–561  ·  view source on GitHub ↗
(&self, num: f64)

Source from the content-addressed store, hash-verified

493 }
494
495 pub fn format_float(&self, num: f64) -> String {
496 let sign_string = if num.is_sign_negative() && !num.is_nan() {
497 "-"
498 } else {
499 self.flags.sign_string()
500 };
501
502 let precision = match &self.precision {
503 Some(CFormatPrecision::Quantity(quantity)) => match quantity {
504 CFormatQuantity::Amount(amount) => *amount,
505 CFormatQuantity::FromValuesTuple => 6,
506 },
507 Some(CFormatPrecision::Dot) => 0,
508 None => 6,
509 };
510
511 let CFormatType::Float(format_type) = self.format_type else {
512 unreachable!()
513 };
514
515 let magnitude = num.abs();
516 let case = format_type.case();
517
518 let magnitude_string = match format_type {
519 CFloatType::PointDecimalLower | CFloatType::PointDecimalUpper => float::format_fixed(
520 precision,
521 magnitude,
522 case,
523 self.flags.contains(CConversionFlags::ALTERNATE_FORM),
524 ),
525 CFloatType::ExponentLower | CFloatType::ExponentUpper => float::format_exponent(
526 precision,
527 magnitude,
528 case,
529 self.flags.contains(CConversionFlags::ALTERNATE_FORM),
530 ),
531 CFloatType::GeneralLower | CFloatType::GeneralUpper => {
532 let precision = if precision == 0 { 1 } else { precision };
533 float::format_general(
534 precision,
535 magnitude,
536 case,
537 self.flags.contains(CConversionFlags::ALTERNATE_FORM),
538 false,
539 )
540 }
541 };
542
543 if self.flags.contains(CConversionFlags::ZERO_PAD) {
544 let fill_char = if !self.flags.contains(CConversionFlags::LEFT_ADJUST) {
545 '0'
546 } else {
547 ' '
548 };
549 format!(
550 "{}{}",
551 sign_string,
552 self.fill_string(

Callers

nothing calls this directly

Calls 9

format_fixedFunction · 0.85
format_exponentFunction · 0.85
format_generalFunction · 0.85
sign_stringMethod · 0.80
absMethod · 0.80
caseMethod · 0.80
fill_stringMethod · 0.80
is_nanMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected