Formating dates & numbers
"Paul Ingendorf" <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.tini |
|---|---|
| Message-ID | <[email protected]> |
Quick background I've been messing around with the ds1822 1-wire device and a 4x20 LCD. I now want to display the fahrenheit equivalent to the celsius measurement along with the current date/time on a 4x20 LCD display. tempF = tempC * 9 / 5 + 32; With this I got values that were to large given the precision. I thought I would fix this using some type of casting to round the numbers to appropriate positions. tempF = ((int)(tempC * 9 / 5 + 32)*100)/100.0; This worked great for everything but the occasional 79.1000000000000001 which is far out of the range of the fairly consistent output now of XX.XX I would appreciate any help with regards towards this interesting issue or alternatives I might explore at truncating the number to my required position. Another option I explored was using a substr but I kept getting errors on dereferencing a double so I thought this would be the best avenue. Another issue I have had is with the lack of date/time extraction. Using the systronix demo tini I wrote a webserver that interfaced the RTC for fun and found it very easy to work with. However when using 1.12 and the 400 vs 390 I found that I cannot use anything worth wild to parse date/time information. The only way I have found to effectively work with the date/time information I found in the archives. They stated at the time to try using the Gregorian object to parse date/time information. So, I used the following code: Calendar calendar = new GregorianCalendar(); Date today = new Date(); calendar.setTime(today); String timeLCD = calendar.get(Calendar.YEAR) + "-" + ((calendar.get(Calendar.MONTH)<10)?"0":"") + calendar.get(Calendar.MONTH) + "-" + ((calendar.get(Calendar.DAY_OF_MONTH)<10)?"0":"") + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR) + ":" + ((calendar.get(Calendar.MINUTE)<10)?"0":"") + calendar.get(Calendar.MINUTE) + " " + ((calendar.get(Calendar.AM_PM)==1)?"PM":"AM"); Notice a lack of seconds. That is because it can take up to 5 seconds to parse this string. Is there a better way? With the systronix demo I could use getHour getMinute etc... and the same logic for adding zeros didn't cost more than a few ms maybe 10-50ms for a total execution time of way less that 200ms vs. what I'm using now at ~5000ms. _______________________________________________ TINI mailing list TINI-6tN4nzCoH/[email protected] To UNSUBSCRIBE, edit your profile, or see list archives: http://lists.dalsemi.com/mailman/listinfo/tini