sprintf convert help

  • Thread starter Thread starter William Stacey [MVP]
  • Start date Start date
W

William Stacey [MVP]

If val is a long, what is the String format conversion of this in c#? TIA!

sprintf(retbuf,"%d.%.2d", val/100, val%100);
 
Hi,
Without a regular expression, it could look like this
retbuf = Math.Abs(val/100) + "." + val%100;

But this will give the same result

retbuf = Math.Round((decimal)val/100,2).ToString();


Good Luck
Adnan
 
Hi
Hi,
Without a regular expression, it could look like this
retbuf = Math.Abs(val/100) + "." + val%100;

But this will give the same result

retbuf = Math.Round((decimal)val/100,2).ToString();


Good Luck
Adnan

Adnan solution can hit in your demand, but you should
look for "sprintf" equivalent in C# String.Format(...)
<or Double.ToString(...)>.

More precise information you can find in "Formatting Overview"
& "NumberFormatInfo" in .NET docs.

I can recomend:

// eg.
val=1234560;

double valDbl=val/100.0;
string strVal1=valDbl.ToString("0.00");

// or

string strVal2=String.Format("{0}.{1}"
, val/100
, ((int)(val%100)).ToString("00"));

Regards

Marcin
 
William said:
If val is a long, what is the String format conversion of this in c#? TIA!

sprintf(retbuf,"%d.%.2d", val/100, val%100);

Something like:

retbuf = String.Format( "{0}.{1:00}", val/100, val%100);
 
Back
Top