2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
6 #include "FloatValueFormatter.h"
14 #include "FloatValue.h"
17 FloatValueFormatter::FloatValueFormatter()
24 FloatValueFormatter::~FloatValueFormatter()
30 FloatValueFormatter::FormatValue(Value
* _value
, BString
& _output
)
32 FloatValue
* value
= dynamic_cast<FloatValue
*>(_value
);
37 BVariant variantValue
= value
->GetValue();
38 switch (variantValue
.Type()) {
41 snprintf(buffer
, sizeof(buffer
), "%f", variantValue
.ToFloat());
46 snprintf(buffer
, sizeof(buffer
), "%g", variantValue
.ToDouble());
51 _output
.SetTo(buffer
);
58 FloatValueFormatter::SupportsValidation() const
65 FloatValueFormatter::ValidateFormattedValue(const BString
& input
,
68 ::Value
* value
= NULL
;
69 return _PerformValidation(input
, type
, value
, false) == B_OK
;
74 FloatValueFormatter::GetValueFromFormattedInput(const BString
& input
,
75 type_code type
, Value
*& _output
) const
77 return _PerformValidation(input
, type
, _output
, true);
82 FloatValueFormatter::_PerformValidation(const BString
& input
, type_code type
,
83 ::Value
*& _output
, bool wantsValue
) const
85 const char* text
= input
.String();
86 char *parseEnd
= NULL
;
87 double parsedValue
= strtod(text
, &parseEnd
);
88 if (parseEnd
- text
< input
.Length() && !isspace(*parseEnd
))
95 newValue
.SetTo((float)parsedValue
);
100 newValue
.SetTo(parsedValue
);
107 _output
= new(std::nothrow
) FloatValue(newValue
);