on floating-point numbers

Dennis Lee Bieber wlfraed at ix.netcom.com
Sun Sep 5 13:22:58 EDT 2021


On Sat, 04 Sep 2021 10:40:35 -0300, Hope Rouselle <hrouselle at jevedi.com>
declaimed the following:

>course, I don't even have floats to worry about.)  If I'm given 1.17,
>say, I am not confident that I could turn this number into 117 by
>multiplying it by 100.  And that was the question.  Can I always
>multiply such IEEE 754 dollar amounts by 100?
>

	HOW are you "given" that 1.17? If that is coming from some
user-readable source (keyboard entry, text-based file [CSV/TSV, even SYLK])
you do NOT have a number -- you have a string, which needs to be converted
by some algorithm.

	For money, the best solution, again, is to use the Decimal module and
feed the /string/ to the initialization call. If you want to do it
yourself, to get a scaled integer, you will have to code a
parser/converter.

	*	strip extraneous punctuation ($, etc -- but not comma, decimal
point, or + and -)
	*	strip any grouping separators (commas, but beware, some countries
group using a period -- "1.234,56" vs "1,234.56"). "1,234.56" => "1234.56"
	*	ensure there is a decimal point (again, you may have to convert a
comma to decimal point), if not append a "." to the input
	*	append enough 0s to the end to ensure you have whatever scale
factor you are using behind the decimal point (as mentioned M$ Excel money
type uses four places) "1234.56" => "1234.5600"
	*	remove the decimal marker. "1234.5600" => "12345600"
	*	convert to native integer. int("12345600") => 12345600 [as integer]
{may fail if +/- are not in the required position for Python}

	If the number is coming from some binary file format, it is already too
late. And you might need to study any DBMS being used. Some transfer values
as text strings, and reconvert to DBMS numeric types on receipt. Make sure
the DBMS is using a decimal number type and not a floating type for the
field (which leaves out SQLite3 <G> which will store anything in any field,
but uses some slightly obscure logic to determine what conversion is done)


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Python-list mailing list