Let's Make Robots!

How to artificially insert a decimal point on a display

I remember seeing this somewhere, but for the life of me, I can't seem to find it again.

I have some values, in my case, battery voltage and the amp draw from my motors. To keep the resolution, but loose the float, things get multiplied by 10. For example, my voltage within the program would be considered 117. When I display this value on my screen, I want to see 11.7 volts. I need to artificially add that decimal point --only so I, a human, can read it more easily.

Now, I think there is something about dividing by 100, then 10 or something. I started thinking this way but quickly ran into the issue of how the result will be "rounded". I guess there might be a solution in dealing with the number as a string and counting the chars, but I dunno.

These has had to come up before, can I get a nudge in the right direction?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

i dont know what platform you are using, but here is how it's done to split the number.

you have 117

to get the first digit devise by 100 you will get 1 with abandoned remainder which is 17. Store 1 in a variable.

then do 117 modulus 100 you will get 17

devise 17 by 10 you will get 1 with 7 as an abandoned remainder. store the one in another variable.

finally  17 modulus 10 will give you 7 store that in another variable .

now you get  v1=1

                   v2=1

                   v3=7

Assemble them as you wish ! :)

Arduino: http://www.arduino.cc/en/Reference/Modulo      % is the modulus operator

Picaxe:  // is the modulus operator

Cheers

To make the algorithm more general if you don't know how big your number is you can determine the number of digits by taking the log of the number, adding one and casting it as an integer, like this by D = [log(x) + 1](int). You can then use that to start  a for loop that will do Jad's algorithm as many times as required.

Cheers.

PS (I assumed your number is in base 10)

No rounding? How do I not know this? After 2+ years of this stuff and the whole time I thought things were being rounded. Huh?

You are of course right, one simple test program and sure enough, no rounding. I am now mentally going through every problem I have ever had and trying to figure out if it was because of this issue.

Man, I can't believe I thought things were being rounded... So simple...

I would convert the 117 into a string and the build the output from there. Its simple and straight forward.

I also guess that it execute much faster than using a lot of division and multiplication code. And no rounding problems to worry about.

only if you know how many decimal places you will be working with in the numbers you are converting.

I believe Jad-Berro's suggestion is used in converting long numbers into individual digits to be printed to a 44780 compatible LCD.

When displaying internal values from my electricity meter, I decided to "invent" units like hecto Watt and deci second. They are mathematically correct and are easily abbreviated using SI conventions.

I love you all, I really do...     ...This was fixed, coded and incorporated two days ago! It really comes down to the fact that I simply didn't know that rounding was not being done. I just always sorta figured that 19/10=2. What the hell, you learn something everyday.

As we sit now, the simple /10 method is working A-ok. I always know how many digits I'm going to get and the decimal place will never change. This is not to say that in the future I am going to need something more complex. I like birdmun's idea of that extra variable --I always dig good "universal" routines where you can stick anything you want into it and it will know what to do with it. Or in this case, be able to ask for different forms of output with the same few lines of code.

So we're groovy. I have my volts and amp draw (for each motor) up on my display (with the aforementioned decimal) and I'm all set.

I should take a moment and plug the Wild Thumper controller though. I have sorta "gotten back to it" in terms of finishing up some of the code. (This is what led to the decimal question) It is now my servo driver, ADC input slave and motordriver. All the volt and current data is coming from it and so far, has been spot-on. I mean, checked with a good meter, spot-on numbers. Speaking of which, it was the fact that I could see current draw on my display that let me know that one of my motors decided to start drawing 13+ amps and needed to be fixed. Damn good motor driver.

 

I think that is Existentialism.

Giants and Lilliputians. They all think they have the standard size. Mega or milli.

We of course all know Gulliver's size is the only one that does not require an SI prefix!

Just added some racism to your existentialism, I'm afraid....