Solved

Casting

Posted on 1998-09-23
5
357 Views
Last Modified: 2010-04-15
Hi,

I have an array of float data type of which I tried to change them to round numbers by casting them to unsigned char data type. But for data such as 3.99, the result of the casting is 3. What can I do so that all the data will be rounded to their nearest values?

Rgds,
MY
0
Comment
Question by:misumi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 10 total points
ID: 1253006
use floor and ceil.

double x=3.99;
char c;

c=(char)ceil(x);

0
 
LVL 84

Expert Comment

by:ozo
ID: 1253007
It won't be the nearest value if you have

double x=3.01;
char c;

c=(char)ceil(x);
0
 

Author Comment

by:misumi
ID: 1253008


So, what should I do? Must I use both ceil and floor together?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253009
You might consider
c=(char)ceil(x-0.5);
0
 
LVL 2

Expert Comment

by:rayb
ID: 1253010
Or a variation on ozo's last response:
int n= (int)(your_array_of_floats[index] + 0.5);
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question