how to convert string timestamp from gregorian to shamsi - android

hello i'am getting a timestamp from server in gregorian what look likes this
{
"error": false,
"users": [
{
"id": 1,
"username": "dfsg",
"active": 1,
"created_at": "2017-02-14 00:13:25"
},
{
"id": 2,
"username": "asf",
"active": 1,
"created_at": "2017-02-14 13:42:26"
},
{
"id": 3,
"username": "test2",
"active": 1,
"created_at": "2017-02-15 13:57:31"
}
]
}
and i want to change it to shamsi what should look like this
1395-11-26 13:42:26
is there any class to do it ? thanks

you could add this class in your project, source
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersianDate {
public String todayShamsi()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = sdf.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
int Y = Integer.valueOf(year);
int M = Integer.valueOf(month);
int D = Integer.valueOf(day);
return Shamsi(Y, M, D);
}
public static String Shamsi(int Y, int M, int D)
{
if (Y == 0)
Y = 2000;
if (Y < 100)
Y = Y + 1900;
if (Y == 2000)
{
if (M > 2)
{
SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = temp.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
Y = Integer.valueOf(year);
M = Integer.valueOf(month);
D = Integer.valueOf(day);
}
}
if (M < 3 || (M == 3 && D < 21))
{
Y -= 622;
}
else Y -= 621;
switch (M)
{
case 1: if (D < 21)
{
M = 10;
D = D + 10;
}
else
{
M = 11;
D -= 20;
}
break;
case 2: if (D < 20)
{
M = 11;
D = D + 11;
}
else
{
M = 12;
D -= 19;
}
break;
case 3:
if (D < 21)
{
M = 12;
D = D + 9;
}
else
{
M = 1;
D -= 20;
}
break;
case 4:
if (D < 21)
{
M = 1;
D = D + 11;
}
else
{
M = 2; D = D - 20;
}
break;
case 5:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 6:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 7:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 8:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 9:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 10:
if (D < 23)
{
M = 7;
D = D + 8;
}
else
{
M = 8;
D = D - 22;
}
break;
case 11:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 12:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
}
String month = "00";
String day = "00";
//D = Integer.valueOf(D)+1;
if (M < 10)
{
month = "0" + M;
}
else
{
month = String.valueOf(M);
}
if (D < 10)
{
day = "0" + D;
}
else
{
day = String.valueOf(D);
}
return String.valueOf(Y) + "/" + month + "/" + day;
}
}

Use Joda Time
As such:
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2013, 3, 31, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);
System.out.println(todayHijri); // 1434-05-19
To get the year, month and day from your string use the following:
String startDateString = "2017-02-14 00:13:25";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(startDateString);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
//Add one to month {0 - 11}
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);

Related

When the image is saved , the image gets rotated by 90 degrees.Using CameraX

private void startCamera() {
CameraX.unbindAll();
Rational aspectRatio = new Rational (textureView.getWidth(), textureView.getHeight());
Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen
PreviewConfig pConfig = new PreviewConfig.Builder()
.setTargetAspectRatio(aspectRatio)
.setTargetResolution(screen)
.setLensFacing(CameraX.LensFacing.BACK)
.build();
Preview preview = new Preview(pConfig);
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
#Override
public void onUpdated(Preview.PreviewOutput output){
ViewGroup parent = (ViewGroup) textureView.getParent();
parent.removeView(textureView);
parent.addView(textureView, 0);
textureView.setSurfaceTexture(output.getSurfaceTexture());
int a=output.getRotationDegrees();
Log.d( TAG , "output Rota3: " + String.valueOf( a) );
updateTransform();
}
});
ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation((getWindowManager().getDefaultDisplay().getRotation())).setFlashMode( FlashMode.OFF ).build();
imgCap = new ImageCapture(imageCaptureConfig);
imgCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + "IMG_" +System.currentTimeMillis() + ".png");
imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
#Override
public void onImageSaved(#NonNull File file) {
String msg = "Pic captured at " + file.getAbsolutePath();
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
}
#Override
public void onError(#NonNull ImageCapture.UseCaseError useCaseError, #NonNull String message, #Nullable Throwable cause) {
String msg = "Pic capture failed : " + message;
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
if(cause != null){
cause.printStackTrace();
}
}
});
}
});
CameraX.bindToLifecycle((LifecycleOwner)this, preview, imgCap);
}
private void updateTransform(){
Matrix mx = new Matrix();
float w = textureView.getMeasuredWidth();
float h = textureView.getMeasuredHeight();
float cX = w / 2f;
float cY = h / 2f;
int rotationDgr;
int rotation = (int)textureView.getRotation();
Log.d( TAG , "rotation: " + rotation );
switch(rotation){
case Surface.ROTATION_0:
rotationDgr = 0;
break;
case Surface.ROTATION_90:
rotationDgr = 90;
break;
case Surface.ROTATION_180:
rotationDgr = 180;
break;
case Surface.ROTATION_270:
rotationDgr = 270;
break;
default:
return;
}
Log.d( TAG , "rotationDGR: " + rotationDgr );
mx.postRotate((float)rotationDgr, cX, cY);
textureView.setTransform(mx);
}
Used CameraX.
Working on application to capture the image and get it saved after capturing.
But unfortunately, I am, getting orientation 0 as the value of getting TargetRotaion() always in every device. Even in the 90 degree rotated image devices.
The problem is image gets rotated on getting saved.
I tried some solutions suggested on StackOverflow but couldn't get the problem resolved.
Use this class `
public class DeviceOrientation {
private static final String TAG = "CameraExif";
// Returns the degrees in clockwise. Values are 0, 90, 180, or 270.
public static int getOrientation(byte[] jpeg) {
if (jpeg == null) {
return 0;
}
int offset = 0;
int length = 0;
// ISO/IEC 10918-1:1993(E)
while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) {
int marker = jpeg[offset] & 0xFF;
// Check if the marker is a padding.
if (marker == 0xFF) {
continue;
}
offset++;
// Check if the marker is SOI or TEM.
if (marker == 0xD8 || marker == 0x01) {
continue;
}
// Check if the marker is EOI or SOS.
if (marker == 0xD9 || marker == 0xDA) {
break;
}
// Get the length and check if it is reasonable.
length = pack(jpeg, offset, 2, false);
if (length < 2 || offset + length > jpeg.length) {
Log.e(TAG, "Invalid length");
return 0;
}
// Break if the marker is EXIF in APP1.
if (marker == 0xE1 && length >= 8 &&
pack(jpeg, offset + 2, 4, false) == 0x45786966 &&
pack(jpeg, offset + 6, 2, false) == 0) {
offset += 8;
length -= 8;
break;
}
// Skip other markers.
offset += length;
length = 0;
}
// JEITA CP-3451 DeviceOrientation Version 2.2
if (length > 8) {
// Identify the byte order.
int tag = pack(jpeg, offset, 4, false);
if (tag != 0x49492A00 && tag != 0x4D4D002A) {
Log.e(TAG, "Invalid byte order");
return 0;
}
boolean littleEndian = (tag == 0x49492A00);
// Get the offset and check if it is reasonable.
int count = pack(jpeg, offset + 4, 4, littleEndian) + 2;
if (count < 10 || count > length) {
Log.e(TAG, "Invalid offset");
return 0;
}
offset += count;
length -= count;
// Get the count and go through all the elements.
count = pack(jpeg, offset - 2, 2, littleEndian);
while (count-- > 0 && length >= 12) {
// Get the tag and check if it is orientation.
tag = pack(jpeg, offset, 2, littleEndian);
if (tag == 0x0112) {
// We do not really care about type and count, do we?
int orientation = pack(jpeg, offset + 8, 2, littleEndian);
switch (orientation) {
case 1:
return 0;
case 3:
return 180;
case 6:
return 90;
case 8:
return 270;
}
Log.i(TAG, "Unsupported orientation");
return 0;
}
offset += 12;
length -= 12;
}
}
Log.i(TAG, "Orientation not found");
return 0;
}
private static int pack(byte[] bytes, int offset, int length,
boolean littleEndian) {
int step = 1;
if (littleEndian) {
offset += length - 1;
step = -1;
}
int value = 0;
while (length-- > 0) {
value = (value << 8) | (bytes[offset] & 0xFF);
offset += step;
}
return value;
}
}`
and use this method in after taking photo
int orientation = DeviceOrientation.getOrientation(bytes);

Print reverse pyramid of numbers

I am able to print pyramid like this :
1
123
12345
1234567
Code i used to print the pyramid of numbers like above is :
int a = 1;
int b = 4;
for (int i = 1 ; i <= 4 ; i++){
for (int c = 1 ; c <= b - 1 ; c++){
text.append(" ");
}
for (int k = 1 ; k <= a ; k++){
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}
but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :
1234567
12345
123
1
Any help would be appreciated?
try this ...
for (int i = 7; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");
if (i % 2 != 0) {
i = i - 1;
}
}
Try this
int a = 1;
int b = 4;
for (int i = 1; i <= 4; i++) {
for (int k = a; k >= 1; k--) {
String result = String.valueOf(k);
text.append(result);
}
for (int c = 1; c <= b - 1; c++) {
text.append(" ");
}
a = a + 2;
b--;
text.append("\n");
}
System.out.println(text.reverse());
Try this :)
int a = 1;
int b = 4;
int m = 4;
for (int i = 1; i <= 4; i++) {
for (int c = 1; c <= m; c++) {
text.append(" ");
}
for (int k = 1; k <= b; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
m++;
text.append("\n");
}
int numberofdigits = 7;
int numberofdigitsforrow = 0;
int Emptyspace = 0;
for (int i = 0; i < numberofdigits; i++)
{
numberofdigitsforrow = numberofdigits - i * 2;
if (numberofdigitsforrow > 0)
{
Emptyspace = numberofdigits-numberofdigitsforrow;
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace/2; b++)
{
Console.Write(" ");
}
}
for (int c = 1; c <= numberofdigitsforrow; c++)
{
Console.Write(c.ToString());
}
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace / 2; b++)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
int w = 7;
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= 5; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = 1; j <=w; j++)
{
System.out.print(j+" ");
}
w = w - 2;
System.out.println();
}

convert gregorian Date to hijri date is back wrong days

I want to convert from Gregorian to Hijri(Islamic) date and I use this class
the problem is islamic month's days is <29 , 30 , 29 , 30 else
but this class month's days is < 30, 29, 30, 29 else
any body can help fix this?
import java.util.Calendar;
/**
* Gregorian-Hijri Dates Converter
*
*
* This Code is used to convert Gregorian dates to Hijri Dates
*
*
*/
public class DateHigri {
static double gmod(double n,double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj=0;
if(adjust){
adj=0;
}else{
adj=1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + day
+ b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = {"Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt"};
String[] iMonthNames = {"Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"};
// This Value is used to give the correct day +- 1 day
boolean dayTest=true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5] + " "
+ iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}

Gregorian to Hijri (Islamic) date

Is there any way or library in Android to convert Gregorian date to Hijri(Islamic) one? I found some Java libraries such as Joda Time but I need something which works in Android.
I found this but I didn't test it yet
public class DateHijri {
static double gmod(double n, double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj = 0;
if (adjust) {
adj = 0;
} else {
adj = 1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716))
+ Math.floor(30.6001 * (m + 1)) + day + b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = { "Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt" };
String[] iMonthNames = { "Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja" };
// This Value is used to give the correct day +- 1 day
boolean dayTest = true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5]
+ " " + iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}
You can use this Class in your Project (Hope it helps) :
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersianDate {
public String todayShamsi()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = sdf.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
int Y = Integer.valueOf(year);
int M = Integer.valueOf(month);
int D = Integer.valueOf(day);
return Shamsi(Y, M, D);
}
public static String Shamsi(int Y, int M, int D)
{
if (Y == 0)
Y = 2000;
if (Y < 100)
Y = Y + 1900;
if (Y == 2000)
{
if (M > 2)
{
SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = temp.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
Y = Integer.valueOf(year);
M = Integer.valueOf(month);
D = Integer.valueOf(day);
}
}
if (M < 3 || (M == 3 && D < 21))
{
Y -= 622;
}
else Y -= 621;
switch (M)
{
case 1: if (D < 21)
{
M = 10;
D = D + 10;
}
else
{
M = 11;
D -= 20;
}
break;
case 2: if (D < 20)
{
M = 11;
D = D + 11;
}
else
{
M = 12;
D -= 19;
}
break;
case 3:
if (D < 21)
{
M = 12;
D = D + 9;
}
else
{
M = 1;
D -= 20;
}
break;
case 4:
if (D < 21)
{
M = 1;
D = D + 11;
}
else
{
M = 2; D = D - 20;
}
break;
case 5:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 6:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 7:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 8:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 9:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 10:
if (D < 23)
{
M = 7;
D = D + 8;
}
else
{
M = 8;
D = D - 22;
}
break;
case 11:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 12:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
}
String month = "00";
String day = "00";
//D = Integer.valueOf(D)+1;
if (M < 10)
{
month = "0" + M;
}
else
{
month = String.valueOf(M);
}
if (D < 10)
{
day = "0" + D;
}
else
{
day = String.valueOf(D);
}
return String.valueOf(Y) + "/" + month + "/" + day;
}
}

Android Renderscript String Functions?

Are there any string functions in Renderscript? Like vsprintf, for example?
Specifically, I'd like to convert a float to a string. Do I have to write that from scratch?
Thanks!
Sorry, here's a better one. It'll work for integers as well but they have ".000" added on.
char stringBuffer[50];
static const int MAX_STRING_LENGTH = sizeof(stringBuffer) - 1;
void drawFloat(float value, int x, int y) {
int index = 0;
int scaledValue = (int)(value * 1000);
index = MAX_STRING_LENGTH;
stringBuffer[index] = 0;
while(scaledValue > 0 || index > MAX_STRING_LENGTH - 4) {
index--;
if(index == MAX_STRING_LENGTH - 4) {
stringBuffer[index--] = '.';
}
int digitValue = scaledValue % 10;
stringBuffer[index] = '0' + digitValue;
scaledValue /= 10;
}
if(value < 0) {
stringBuffer[index--] = '-';
}
rsgDrawText(&stringBuffer[index], x - 10, y + 5);
}
Couldn't find a simple way so...
void drawInteger(int value, int x, int y) {
char text[50] = "0";
int index = 0;
if(value != 0) {
index = 49;
text[index] = 0;
while(value > 0) {
index--;
int digitValue = value % 10;
text[index] = '0' + digitValue;
value /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}
void drawFloat(float value, int x, int y) {
char text[50] = "0.000";
int index = 0;
if(value != 0) {
int integerPart = (int)(value * 1000);
index = 49;
text[index] = 0;
while(integerPart > 0) {
index--;
if(index == 45) {
text[index--] = '.';
}
int digitValue = integerPart % 10;
text[index] = '0' + digitValue;
integerPart /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}

Categories

Resources