Solved

Casting

Posted on 1998-09-23
5
326 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now