Menu

#8 Floating points go wrong when using other locale than en_US

open
nobody
None
5
2013-01-03
2013-01-03
Anonymous
No

When a project requires a different locale setting than en_US the floating points that are inserted into the sheet can be of the wrong format (i.e. 0,5 instead of the required 0.5).
This can be fixed in ExcelWriterXML_Sheet by changing the following lines: (the how and why of the fix is written in the comment)

353 $data = $cell['data'];
354 $data = htmlspecialchars($data);

To

353 $data = $cell['data'];
354 if (is_float($data)) {
355 /* Use sprintf to prevent loss of decimal characters, since
356 * normal float -> string conversion does not always use
357 * the . as a decimal separator. %F does not take into
358 * account locale (%f does). Since sprintf uses only a
359 * precision of up to 6 decimal places by default, we
360 * explicitely provide a precision. */
361 $prec = ini_get('precision');
362 $data = sprintf("%.${prec}F", $data);
363 }
364 $data = htmlspecialchars($data);

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.