mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:07:35 +00:00
AK: Allow %m.nf specifier for double/float in printf to set fraction with
This patch adds the missing part for the printf of double values to specify the length of the fraction part. For GVariant, a default of %.2 is used.
This commit is contained in:
parent
22f20cd51d
commit
154dcd1ed6
2 changed files with 20 additions and 10 deletions
|
@ -178,7 +178,7 @@ template<typename PutChFunc>
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename PutChFunc>
|
template<typename PutChFunc>
|
||||||
[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth)
|
[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth, u32 fraction_length = 6)
|
||||||
{
|
{
|
||||||
int length = 0;
|
int length = 0;
|
||||||
|
|
||||||
|
@ -192,9 +192,11 @@ template<typename PutChFunc>
|
||||||
putch(bufptr, '.');
|
putch(bufptr, '.');
|
||||||
length++;
|
length++;
|
||||||
double fraction = number - (i64)number;
|
double fraction = number - (i64)number;
|
||||||
// FIXME: Allow other fractions
|
|
||||||
fraction = fraction * 1000000;
|
for (u32 i = 0; i < fraction_length; ++i)
|
||||||
return length + print_u64(putch, bufptr, (i64)fraction, leftPad, true, 6);
|
fraction = fraction * 10;
|
||||||
|
|
||||||
|
return length + print_u64(putch, bufptr, (i64)fraction, false, true, fraction_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename PutChFunc>
|
template<typename PutChFunc>
|
||||||
|
@ -295,6 +297,7 @@ template<typename PutChFunc>
|
||||||
bool zeroPad = false;
|
bool zeroPad = false;
|
||||||
bool dot = false;
|
bool dot = false;
|
||||||
unsigned fieldWidth = 0;
|
unsigned fieldWidth = 0;
|
||||||
|
unsigned fraction_length = 0;
|
||||||
unsigned long_qualifiers = 0;
|
unsigned long_qualifiers = 0;
|
||||||
bool size_qualifier = false;
|
bool size_qualifier = false;
|
||||||
(void)size_qualifier;
|
(void)size_qualifier;
|
||||||
|
@ -324,10 +327,17 @@ template<typename PutChFunc>
|
||||||
goto one_more;
|
goto one_more;
|
||||||
}
|
}
|
||||||
if (*p >= '0' && *p <= '9') {
|
if (*p >= '0' && *p <= '9') {
|
||||||
fieldWidth *= 10;
|
if (!dot) {
|
||||||
fieldWidth += *p - '0';
|
fieldWidth *= 10;
|
||||||
if (*(p + 1))
|
fieldWidth += *p - '0';
|
||||||
goto one_more;
|
if (*(p + 1))
|
||||||
|
goto one_more;
|
||||||
|
} else {
|
||||||
|
fraction_length *= 10;
|
||||||
|
fraction_length += *p - '0';
|
||||||
|
if (*(p + 1))
|
||||||
|
goto one_more;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (*p == '*') {
|
if (*p == '*') {
|
||||||
fieldWidth = va_arg(ap, int);
|
fieldWidth = va_arg(ap, int);
|
||||||
|
@ -381,7 +391,7 @@ template<typename PutChFunc>
|
||||||
#if !defined(BOOTSTRAPPER) && !defined(KERNEL)
|
#if !defined(BOOTSTRAPPER) && !defined(KERNEL)
|
||||||
case 'g':
|
case 'g':
|
||||||
case 'f':
|
case 'f':
|
||||||
ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth);
|
ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth, fraction_length);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -398,7 +398,7 @@ String Variant::to_string() const
|
||||||
case Type::UnsignedInt:
|
case Type::UnsignedInt:
|
||||||
return String::number(as_uint());
|
return String::number(as_uint());
|
||||||
case Type::Float:
|
case Type::Float:
|
||||||
return String::format("%f", (double)as_float());
|
return String::format("%.2f", (double)as_float());
|
||||||
case Type::String:
|
case Type::String:
|
||||||
return as_string();
|
return as_string();
|
||||||
case Type::Bitmap:
|
case Type::Bitmap:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue