Click to See Complete Forum and Search --> : Help request on String conversion


Modeller
January 28th, 2005, 07:26 AM
Hi,

I have two requests regarding to String conversion. For the first one, I am enclosing the part of
C++ code. For the second, please see below right after the first request.

System spesifications :

Compiler : Visual C++ . Net, standart, 2003
Operative system : XP Professional

Request 1 : Conversion from String __gc* to floating point number. The desired input number is a
negative float, e.g. -5000. The following throws "Format Exception"

// the variable depth is declared as follows : float depth;

void depthTextBox_TextChanged(System::Object* sender, System::EventArgs* e)
{
try
{
depth = float(Double::Parse(depthTextBox->Text, System::Globalization::NumberStyles::Float));
if (depth > 0)
{
depthTextBox->set_Text(__box(depth)->ToString());
throw new Exception("Depth is a negative number!");
}
errorProvider->SetError(depthTextBox, "");
}
catch (Exception* ex)
{
// If there is an error, display the text
depthTextBox->set_Text(depthTextBox->Text);
this->errorProvider->SetError(depthTextBox,ex->Message);
}
}

void depthTextBox_Validating(System::Object* sender, System::ComponentModel::CancelEventArgs* e)
{
try
{
if (depthTextBox->Text->Length == 0)
throw new Exception("Depth is required!");
}
catch (Exception* ex)
{
e->Cancel = true;
this->errorProvider->SetError(depthTextBox,ex->Message);
}
}

void depthTextBox_Validated(System::Object* sender, System::EventArgs* e)
{
errorProvider->SetError(depthTextBox, "");
}

What is wrong here?

// ********************** Request 2 ***************************

Conversion from floating number to String __gc* : Say that I have the following
floating point number : -1.2345678 and I want to display only the first two decimal digits of it,
such as -1.23. What is the string format that displays this number?

I thank you very much for your interest.

Regards
Modeller

atkin
February 1st, 2005, 01:35 AM
hi
to convert from a floating point to a string you can use
using namespace System;
float test;
String testString = System::Convert::ToString(test);

hope this helps

Modeller
February 2nd, 2005, 04:09 PM
Hi,

Thanks for the suggestion. However, I was wondering how to write only a portion of a floating number, e.g. given -123.456789 I only want to write the following part -123.45.

I agree that CONVERT might help, but I do not know the String::Format that will be used in the conversion.

Thanks anyway!