MCPcopy Create free account
hub / github.com/argotorg/solidity / formatNumberReadable

Method formatNumberReadable

libsolutil/StringUtils.cpp:158–197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

156}
157
158std::string solidity::util::formatNumberReadable(bigint const& _value, bool _useTruncation)
159{
160 bool const isNegative = _value < 0;
161 bigint const absValue = isNegative ? (bigint(-1) * _value) : bigint(_value);
162 std::string const sign = isNegative ? "-" : "";
163
164 // smaller numbers return as decimal
165 if (absValue <= 0x1000000)
166 return sign + absValue.str();
167
168 if (auto result = tryFormatPowerOfTwo(absValue))
169 return {sign + *result};
170 else if (auto result = tryFormatPowerOfTwo(absValue + 1))
171 return {sign + *result + (isNegative ? " + 1" : " - 1")};
172
173 std::string str = toHex(toCompactBigEndian(absValue), HexPrefix::Add, HexCase::Mixed);
174
175 if (_useTruncation)
176 {
177 // return as interior-truncated hex.
178 size_t len = str.size();
179
180 if (len < 24)
181 return sign + str;
182
183 size_t const initialChars = 6;
184 size_t const finalChars = 4;
185 size_t numSkipped = len - initialChars - finalChars;
186
187 return fmt::format(
188 "{}{}...{{+{} more}}...{}",
189 sign,
190 str.substr(0, initialChars),
191 numSkipped,
192 str.substr(len-finalChars, len)
193 );
194 }
195
196 return sign + str;
197}
198
199std::string solidity::util::prefixLines(
200 std::string const& _input,

Callers

nothing calls this directly

Calls 5

tryFormatPowerOfTwoFunction · 0.85
toHexFunction · 0.85
toCompactBigEndianFunction · 0.85
strMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected